Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
500 server error in google app engine. what's the problem?
I want deploy django + cloudsql + app engine yaml is enter image description here loud_sql_instances and requirements.txt : Django, psycopg2, psycopg2-binary, Gunicorn However, 500 server error occurs when executed, and no error message occurs. What's the reason ? -
List Serializer into dict with dynamic keys: is there a simpler way?
I'm working on a platform where users can list clothing products, and each listing has a unique set of product-size-quantity relationships. The relevant parts of the model are as follows: class ProductSizeVariation(models.Model): base_product = models.ForeignKey(BaseProduct) size = models.TextField() class ProductListing(models.Model): base_product = models.ForeignKey(BaseProduct) # other attributes such as owner, creation date, etc class ProductListingSizeQuantity(models.Model): product_listing = models.ForeignKey(ProductListing) quantity = models.IntegerField() Using a simple ModelSerializer for ProductListingSize I would get a representation of sizes and quantities like so: [{'size': 40, 'quantity': 3}, {'size': 42, 'quantity': 5}] I would like however a dict representation of the form: {'40': 3, '42': 5} I have a working solution, inspired by drf-keyed-list, whose relevant parts are: class KeyedListSerializer(ListSerializer): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) meta = getattr(self.child, 'Meta', None) # ... assert that keyed_field and value_field have been provided self._keyed_field = meta.keyed_list_serializer_field self._value_field = meta.value_list_serializer_field def to_internal_value(self, data): # ... various validation checks data = [{self._keyed_field: k, self._value_field: v} for k, v in data.items()] return super().to_internal_value(data) def to_representation(self, data): response = super().to_representation(data) return {v.pop(self._keyed_field): v[self._value_field] for v in response} class ProductListingSizeQuantitySerializer(serializers.ModelSerializer): size = serializers.CharField(source='product_size_variation.size') class Meta: model = ProductListingSizeQuantity list_serializer_class = KeyedListSerializer fields = ('size', 'quantity') keyed_list_serializer_field = 'size' value_list_serializer_field = 'quantity' class ProductListingSerializer(serializers.ModelSerializer): product_listing_sizes … -
How to get Django LoginRequiredMiddleware to return a 401_UNAUTHORIZED code instead of 302_FOUND
I am writing a custom basic LoginRequiredMiddleware for my Django project and so far everything works as expected. I am using process_request because I thought I wanted the request to be checked for authentication before the view is processed. However, this middleware fails at a test that other developers have written: self.assertEqual(resp.status_code, status.HTTP_401_UNAUTHORIZED) AssertionError: 302 != 401 This happens because the view is discovered even if the user is not authenticated when it should actually return a status.HTTP_401_UNAUTHORIZED. How can I rewrite my middleware to comply with the test and return the correct status code? middleware.py import re from django.conf import settings from django.shortcuts import redirect from django.contrib.auth import logout from django.utils.deprecation import MiddlewareMixin EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))] if hasattr(settings, 'LOGIN_EXEMPT_URLS'): EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS] class LoginRequiredMiddleware(MiddlewareMixin): def process_request(self, request): assert hasattr(request, 'user') path = request.path_info.lstrip('/') url_is_exempt = any(url.match(path) for url in EXEMPT_URLS) if path == settings.LOGOUT_URL.lstrip('/'): logout(request) if request.user.is_authenticated and url_is_exempt: return redirect(settings.LOGIN_REDIRECT_URL) elif request.user.is_authenticated or url_is_exempt: return None else: return redirect(f"{settings.LOGIN_URL}?next=/{path}") -
how can set secret kay and host in django environment os.environ
i use os.environ['SECRET_KEY'] and how can i put my secret key in environment how it works i get this error ● gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2021-02-01 09:24:43 UTC; 1h 13min ago TriggeredBy: ● gunicorn.socket Process: 66236 ExecStart=/home/user/venv/bin/gunicorn django_project.wsgi (code=exited, status=3) Main PID: 66236 (code=exited, status=3) Status: "Gunicorn arbiter booted" Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: File "/var/www/html/web/django_project/settings.py", line 23, in <module> Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: SECRET_KEY = os.environ['SECRET_KEY'] Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: File "/usr/lib/python3.8/os.py", line 675, in __getitem__ Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: raise KeyError(key) from None Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: KeyError: 'SECRET_KEY' Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66248]: [2021-02-01 09:24:43 +0000] [66248] [INFO] Worker exiting (pid: 66248) Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66236]: [2021-02-01 09:24:43 +0000] [66236] [INFO] Shutting down: Master Feb 01 09:24:43 ip-123-44-51-87 gunicorn[66236]: [2021-02-01 09:24:43 +0000] [66236] [INFO] Reason: Worker failed to boot. Feb 01 09:24:43 ip-123-44-51-87 systemd[1]: gunicorn.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED Feb 01 09:24:43 ip-123-44-51-87 systemd[1]: gunicorn.service: Failed with result 'exit-code'. -
How can I validate Iranian National code in python
I want to validate the Iranian National code in my python code and I couldn't find the right pattern to do it -
Django: Make a Queryset with annotate using an object's property
I have a queryset in my Django views.py. I'm using annotate to add a promo property to each object based on each object's id. However this is not working: bytes = Byte.objects.filter( published=True ).annotate( # add path to promo image based on byte's id property promo=Value('../static/images/promo-'+id+'.png', output_field=CharField()) ).order_by( '-publish_date' ) I get the error: name 'id' is not defined -
How to get difference between 2 querysets in django
I have 2 fields in my models i.e., class Payment(models.Model): tasks_requested = models.ManyToManyField(Task, null=True, blank=True) tasks_accepted = models.ManyToManyField(Task, null=True, blank=True, related_name='tasks_accepted') I want to get the difference of these querysets in models save method, when someone create the object from admin panel task_left = self.tasks_requested.all() - self.tasks_accepted.all() The above code would not work can someone suggest a right way to do so -
how to sort the rows according to the particular column value in python Django
we have a table and we want to print the table according to the status. Status is one of the column name. That status should be ok, unreachable, alert and we want the row whose status is ok that will show on top. so my question is where i have to write the code i.e inside the model.py or view.py . def get_queryset(self, request): qs = self.model._default_manager.get_queryset() order = ['Ok', 'Alert', 'Unreachable'] return sorted(qs, key=lambda x: order[x[status]]) i have wrote this code inside the model.py its not working -
Only one polish letter displayed correctly in xhtml2pdf generated pdf with ISO-8859-2 encoding
I am trying to generate PDF with my data in it. I use xhtml2pdf: def render_to_pdf(template_src, context_dict={}): template = get_template(template_src) html = template.render(context_dict) result = BytesIO() pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-2")), result, encoding='ISO-8859-2') if not pdf.err: return HttpResponse(result.getvalue(), content_type='application/pdf') return None ... elif response.POST.get("print"): context = { "ls": ls_query, "page_product": page_product, } pdf = render_to_pdf("print_zam.html", context) if pdf: response = HttpResponse(pdf, content_type='application/pdf') filename = "Zamówienie_%s.pdf" % ls_query content = "inline; filename=%s" % filename response['Content-Disposition'] = content return response return HttpResponse("Not found") In my template i use <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-2"> but the only polish letter that is displayed correctly is Óó, the rest looks like a black square. Where seems to be the issue? -
how can i delete image from file system?
i want to test my api when i test each time create and delete the photo in test database but each time add it for the nth time it add another from that photo to file system so i cant check the result because each time i have another of that photo how i can delete them each time from file system class TestUserAvatar(APITestCase): def setUp(self): user_information() self.user1 = User.objects.first() self.client = APIClient() def test_post_user_avatar(self): self.client.force_login(user=self.user1) with open("/Users/fahime/desktop/download.jpeg", "rb") as fp: response1 = self.client.patch("/api/v1.0.0/accounts/avatar/", {"cover": fp}) -
How to filter an annotated query based on a specific item of the query ,keeping the annotated fields?
I have a table named A.I'm annotating some columns to it and creating a queryset. Now I want to filter this queryset based on one of the records of this table and want to have the correct values of the annotated columns.I don't want the annotated fields to be calculated again. The most important annotated column for me is the row_number column. -
Show fields in admin page only if user has specific rights
I'm working on the Django admin site of an online shop, and I'm asked to enable a refund button only to a limited group of admins. The current situation, extremely simplified, is the following: models.py class ItemOrder(models.Model) [various fields and functions] admin.py class ItemOrderAdmin(admin.ModelAdmin): fieldsets = [ ... ('Actions', {'fields': ['created', 'modified', 'refund_link']}), ] ... readonly_fields = [..., 'created', 'modified', 'refund_link'] [other functions] def refund_link(self, order): ... At the moment every user with the right to see this page can click on the refund link and refund the order. I need to restrict its visibility/usability only to users who have a specific right. I tried with the @permission_required decorator, but the link is unusable for every user (it becomes "-" for anyone, even superusers). I also tried adding the get_form method to the ItemOrderAdmin class, but the refund link disappears completely for every user. This is how it was implemented: admin.py class ItemOrderAdmin(admin.ModelAdmin): fieldsets = [ ... ('Actions', {'fields': ['created', 'modified']}), ] ... readonly_fields = [..., 'created', 'modified', 'refund_link'] def get_form(self, request, obj=None, **kwargs): if request.user.is_superuser: # Will be changed with a more specific right later kwargs['form'] = EventTicketOrderPersonForm return super().get_form(request, obj, **kwargs) [other functions] def refund_link(self, order): ... forms.py … -
Django foreign key JWT auth
In my project I have the Post and Category Model and full working JWT Authentication. class Category(models.Model): name = models.CharField(max_length=255) def __str__(self): return self.name class Post(models.Model): title = models.CharField(max_length=50) content = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) author = models.ForeignKey(User, on_delete=models.CASCADE) category = models.ManyToManyField(Category, related_name='posts')\ class Category(models.Model): name = models.CharField(max_length=255) def __str__(self): return self.name I want to create a view, that creates a new Post object, in which author will be assigned to Token owner that I pass in authorization (Bearer Token ) postman.image.example. I dont know how to do it please help. Sorry for my english. Serializer class PostSerializer(FlexFieldsModelSerializer): class Meta: model = Post fields = '__all__' read_only_fields = ['id', 'created'] expandable_fields = { 'category': ('blog.CategorySerializer', {'many': True}), 'comments': ('blog.CommentSerializer', {'many': True}), 'images': ('blog.ImageSerializer', {'many': True}), } -
Django get_or_create: aggregation as default
For each day, I need to assign an incremental ID to messages that I receive. Messages can get consumed multiple times, in which case the existing ID should be used if it already exists for the current day. For example: date | message_key | assigned_id 2021-02-02 | A | 1 2021-02-02 | B | 2 2021-02-02 | C | 3 2021-02-02 | A | 1 ... 2021-02-03 | D | 1 2021-02-03 | A | 2 Is there any way I can accomplish this using Django while avoiding race conditions? This is what I currently have, but this is not safe from race conditions: @transaction.atomic def get_message_id(message_key: str): today = datetime.date.today().strftime('%Y-%m-%d') counter, created = Counter.objects.select_for_update().get_or_create( date=today, message_key=message_key, defaults={ "assigned_id": Counter.objects.filter(date=today).aggregate( value=Coalesce(Max("assigned_id") + 1, 1) )["value"] } ) return counter.assigned_id -
I am getting django.core.exceptions.ImproperlyConfigured while trying to setup my django and django channels app with daphne
I am getting Django.core.exceptions.ImproperlyConfigured while trying to setup my Django and Django channels app with daphne. This error only comes when I use Django Simple JWT. raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting SIMPLE_JWT, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. -
Django - Do Not send email to admins for HTTP 503 error code
I configured Error reporting for my django app to send emails to admins i specified in ADMINS list in settings.py. it works perfectly well. Based on official django documentation, it will send email whenever a HTTP 500 or greater status code raised: When DEBUG is False, Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (strictly speaking, for any response with an HTTP status code of 500 or greater). How to override this behaviour? To be specific, i want django to send erros just when HTTP 500 raised. P.S: sometimes i enable maintenance mode in my app, so each request will raise a 503 code for user which is OK. But it will send an error email per request for ADMINS which is NOT OK! -
Make function return predictable values for testing purposes
I need a function to return predictable values when running tests. For example, I have a function get_usd_rates(), which is loading USD forex rates from some API. So far when I was using it inside any other function I was passing rates as an optional argument for testing purposes only. Seems hacky, but it worked. Like this: def some_other_function(rates=None): if rates is None: rates = get_usd_rates() # do something with rates But now I am facing a situation where I can't pass extra argument to a function (private class method for django model, which is called on model field change). Is there a way to make get_usd_rates() function aware that test is running and always return some predefined value without noticeable performance impact in this case? Or what is the best way to deal with this problem. -
Im getting the following error : name 'doctor_id' is not defined
I am trying to get objects from a database onto a webpage but I seem to keep getting the following error. The framework I'm using is Django Error: (it occurs in the views.py incase you were wondering) name 'doctor_id' is not defined The views.py is: from django.shortcuts import render, get_object_or_404 from django.core.mail import send_mail from .models import Doctors def test2(request): doctor = Doctors.objects.all() selected_item2 = get_object_or_404(Doctors, pk=doctor_id) Doctors.doctor = selected_item2 Doctors.save() return render(request, 'website/test2.html', {}) The models.py is: class Doctors(models.Model): dname = models.CharField(max_length=80) ddept = models.CharField(max_length=80) dphone = models.CharField(max_length=80) demail = models.CharField(max_length=80) dimage = models.ImageField(upload_to= 'portfolio/images') -
How do I create a search for a foreignkey in Rest framework? I tried and got "FieldError at /list/ Related Field ...: icontains"
this the serializer with the fields added class ListSerializer(serializers.ModelSerializer): user = serializers.ReadOnlyField(source='user.username') status = serializers.ReadOnlyField(source='status.status') title = serializers.ReadOnlyField(source='title.title') class Meta: model = ListModel fields = ('user', 'title', 'status', 'endDate') my views are below with the codes for the search class ListView(generics.ListAPIView): queryset = ListModel.objects.all() serializer_class = ListSerializer filter_backends = [filters.SearchFilter] search_fields = ['user', 'title', 'status'] def perform_create(self, serializer): serializer.save(user=self.request.user, status=self.request.status, title=self.request.title) my model with the foreign key. the date field is the only field that is not a foreign key class ListModel(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='User', on_delete=models.CASCADE) title = models.ForeignKey(DocxModel, on_delete=models.CASCADE, related_name='topic') status = models.ForeignKey(CatModel, on_delete=models.CASCADE, default="TO DO", name='status') endDate = models.DateField(default=strftime("%Y-%m-%d"), name='endDate') def __str__(self): return str(self.user) -
Django: objects.raw() shows error while working with Postgresql but works fine while using sqlite3 database
My code works fine when I use the sqlite3 DB. But when I use Postgresql as my DB my code throws error column "product_variants.id" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT * FROM product_variants WHERE product_id=6 GROUP BY... I have made all necessary migrations already. views.py def product_detail(request,id,slug): query = request.GET.get('q') product = Product.objects.get(pk=id) context = {'product': product,} if product.variant !="None": # Product have variants if request.method == 'POST': #if we select color variant_id = request.POST.get('variantid') variant = Variants.objects.get(id=variant_id) #selected product by click color radio colors = Variants.objects.filter(product_id=id,size_id=variant.size_id ) sizes = Variants.objects.raw('SELECT * FROM product_variants WHERE product_id=%s GROUP BY size_id',[id]) # sizes = Variants.objects.filter(product_id=id).values('size_id') print(sizes) query += variant.title+' Size:' +str(variant.size) +' Color:' +str(variant.color) else: variants = Variants.objects.filter(product_id=id) colors = Variants.objects.filter(product_id=id,size_id=variants[0].size_id ) sizes = Variants.objects.raw('SELECT * FROM product_variants WHERE product_id=%s GROUP BY size_id',[id]) # sizes = Variants.objects.filter(product_id=id).values('size_id') print(sizes) variant =Variants.objects.get(id=variants[0].id) context.update({'sizes': sizes, 'colors': colors, 'variant': variant,'query': query }) # print(product.countreview()) return render(request,'eshop/products.html',context) In the above code, I have tried to replace the .raw() query to .filter() (which I later commented out) but still, it doesn't work as I expected; it doesn't show the size details but shows the color details in … -
Django rest framework POST request: JSONDecodeError at /competition-create
Im trying to simply perform a post request that will save a new competition to the database. # views.py class CompetitionCreateView(generics.CreateAPIView): serializer_class = CompetitionSerializer queryset = Competition.objects.all() def create(self, request, *args, **kwargs): serializer = CompetitionSerializer(data=request.data) if serializer.is_valid(): competition = serializer.save() return Response( data=CompetitionSerializer(competition).data, status=status.HTTP_201_CREATED, content_type="json") return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST, content_type="json") # serializer.py class CompetitionSerializer(serializers.ModelSerializer): class Meta: model = Competition fields = "__all__" response: I have tried using regular APIView and switching from def create(...) to def post(...) but no luck. Also tried to parse the request data content to json. -
ManyToManyField save in Django generic View not saving correctly
I have the following code for a Model that has a ManyToManyField in it. SinglePrescriptionFormset = modelformset_factory( PrescriptionLine, form=PrescriptionLineForm, fields='__all__', extra=0, can_delete=True) class PrescriptionForm(forms.ModelForm): class Meta: model = Prescription exclude = ['handed_out', ] def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper() self.helper.form_tag = True self.helper.form_class = 'form-horizontal' self.helper.label_class = 'col-md-3 create-label' self.helper.field_class = 'col-md-9' self.helper.layout = Layout( Div( Field('patient'), Field('note'), HTML('<hr class="style2">'), Fieldset('Add Drug', Formset('line_prescription')), HTML('<hr class="style1">'), ButtonHolder(Submit('submit', 'save')), ) ) def clean(self, *args, **kwargs): cleaned_data = super().clean(*args, **kwargs) print(cleaned_data) # cleaned_data.get('line_prescription') return cleaned_data class PrescriptionCreate(generic.CreateView): model = Prescription template_name = 'doc_aide/write_prescription4.html' form_class = PrescriptionForm def get_context_data(self, **kwargs): print('here') context = super().get_context_data(**kwargs) if self.request.POST: context['line_prescription'] = SinglePrescriptionFormset(self.request.POST) else: context['line_prescription'] = SinglePrescriptionFormset() context['form'].fields['patient'].initial = Patient.objects.get(pk=self.kwargs['patient']) return context def form_valid(self, form): print('Ia am here') context = self.get_context_data() prescriptionlines = context['line_prescription'] with transaction.atomic(): form.instance.created_by = self.request.user self.object = form.save() if prescriptionlines.is_valid(): prescriptionlines.instance = self.object prescriptionlines.save() return super().form_valid(form) def get_success_url(self): return reverse('doc_aide:prescription_detail', kwargs={'pk': self.object.pk}) This kind of works but there is one mistake in it. It saves the same ManyToManyField to all other Prescription Models. Meaning that when I have created one Prescription it saves all correctly. When I save another the ManyToManyFields from the first one are overwritten. New ones are not … -
Query a m2m relation in django
I have a ORM in which there is a m2m relation like this: class FlowKits(models.Model): kit = models.ForeignKey(Kit, on_delete=models.CASCADE) trip_cost = models.IntegerField(default=0) class Flow(models.Model): flow_name = models.CharField(max_length=500, default=0) kits = models.ManyToManyField(FlowKits) How can I get the trip_cost of a Flow whose id = 10 and kit id = 18? I tried the following but it just give me the Flow Object f_obj = Flow.objects.filter(id=10, kits__kit=18) -
Problem of referencing the body content of the Post model using Foreign Key in Newsletter's model
I am trying to send out the email with the Post body instead of HTML file upload default in sendgrid. Therefore, I changed the original code contents = models.FileField(upload_to='uploaded_newsletters/') to contents = models.ForeignKey(Post, on_delete=models.CASCADE, null=True). But I am not sure whether it can access the Post object's body with this code. And after changing it there is an error of 'Post' object has no attribute 'read' when I tried to send the email. I know this is because the code of contents = self.contents.read().decode('utf-8') . Therefore, I would like to ask how can I change these 2 lines of code in order to send out the Post body in an email? Thanks. My Post model: class Post(models.Model): title = models.CharField(max_length=255) body = models.TextField(blank=True, null=True) def __str__(self): return self.title My Newsletter model: class Newsletter(models.Model): subject = models.CharField(max_length=150) contents = models.ForeignKey(Post, on_delete=models.CASCADE, null=True) #here is my problem def __str__(self): return self.subject + " " + self.created_at.strftime("%B %d, %Y") def send(self, request): contents = self.contents.read().decode('utf-8') ##here is my problem too subscribers = Subscriber.objects.filter(confirmed=True) sg = SendGridAPIClient(settings.SENDGRID_API_KEY) for sub in subscribers: message = Mail( from_email=settings.FROM_EMAIL, to_emails=sub.email, subject=self.subject, html_content=contents + ( '<br><a href="{}/delete/?email={}&conf_num={}">Unsubscribe</a>.').format( request.build_absolute_uri('/delete/'), sub.email, sub.conf_num)) sg.send(message) -
DETAIL: Key (sns_login_id)=(1) is not present in table "social_login_platforms" while unit testing with Pytest
I have been trying to implement unit tests with a legacy codebase. So far, I have created simple fixture functions that create Country, User, SocialLoginPlatform objects. I use Pytest and Pytest-Django to run unit tests. However, there seems to be an absence of some data, which I cannot understand since I didn't forget to put the fixture in the testing function. tests.py @pytest.fixture def social_platform(): SocialLoginPlatform.objects.create(name="none") social_platform = mixer.blend(SocialLoginPlatform, name="google") return social_platform @pytest.fixture def country(): country = mixer.blend(Country, name="South Korea", code="KR") return country @pytest.mark.django_db def test_user_creation_user_signup(client, country, social_platform): url = reverse('signup-admin') data = { "company_name": "Random_Company_02", "country_id": 3, "email": "lnrdwd@goreadit.site", "language": "en", "name": "Rand Minit", "password": "12345678" } response = client.post(url, json.dumps(data), content_type='application/json') assert country.id == 3 The test runs fine and passes the test assert country.id == 3 However, as the test tears down the test_db, an error occurs: ====================================== ERRORS ====================================== _______________ ERROR at teardown of test_user_creation_user_signup ________________ self = <django.db.backends.utils.CursorWrapper object at 0x10b4a0d50> sql = 'SET CONSTRAINTS ALL IMMEDIATE', params = None ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x109f84e10>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x10b4a0d50>}) def _execute(self, sql, params, *ignored_wrapper_args): self.db.validate_no_broken_transaction() with self.db.wrap_database_errors: if params is None: # params default might be backend specific. > return self.cursor.execute(sql) …