django-queryset

Django queryset show english translations

Django queryset show english translations Question: This is my Django models.py: from django.db import models class Blog(models.Model): pub_date = models.DateTimeField(‘date published’) def __str__(self): return self.pub_date class LanguagesCode(models.Model): code = models.CharField(max_length=5) def __str__(self): return self.code class BlogTranslation(models.Model): name = models.CharField(max_length=250) content = models.TextField(blank=True, null=True) blog_id = models.ForeignKey(Blog, related_name=’translations’, on_delete=models.CASCADE) language_code = models.ForeignKey(LanguagesCode, related_name=’languages’, on_delete=models.DO_NOTHING) class Meta: …

Total answers: 2

The 'patch' method generating a 500 error when recieving a pk in the URL

The 'patch' method generating a 500 error when recieving a pk in the URL Question: I’m sending a PATCH request (naturally) passing a pk and I’m getting a TypeError: patch() got an unexpected keyword argument ‘pk’ These are the request parameters I’m sending: Request URL: http://localhost:8000/api/task/toggle-done/1/ Request headers: {‘Authorization’: ‘Token 13f2f18ea582e9c585c817ba52358b5b19e696a8’, ‘Content-Type’: ‘application/json’, ‘Content-Length’: ‘0’} …

Total answers: 2

How to fill test DB in Django with objects of models with ManyToManyFields?

How to fill test DB in Django with objects of models with ManyToManyFields? Question: I want to test speed of my project, so I need to fill my DB with test data. I have a model with lots of ManyToManyFields (later I will create more models): class Deciduous(PlantBasicCharacteristics): usda_zone = models.ManyToManyField(UsdaZone) soil_moisture = models.ManyToManyField(SoilMoisture) soil_fertility …

Total answers: 1

How to check if an identical element exists in another table in Django?

How to check if an identical element exists in another table in Django? Question: I have two models: CaseRequest and Case. The CaseRequest object has values name and datebirth. I need to check if the same values exists for name and datebirth in Case. For instance, The result should be like this: Case: Name DateBirth …

Total answers: 3

How to count the number of elements of each category in Django?

How to count the number of elements of each category in Django? Question: I have a list of elements. There is a ‘status’ value here. I need a code that in an HTML page will show the total number of items for each ‘status’. How to do it? For intance, there is the list in …

Total answers: 1

How to get multiple field values, when a foreign key is called in django

How to get multiple field values, when a foreign key is called in django Question: I have a Attendance model class Attendance(models.Model): employee = models.ForeignKey(Employee, on_delete=models.CASCADE) date = models.DateField() day = models.CharField(max_length=10) in_time = models.CharField(max_length=5) out_time = models.CharField(max_length=5) Here is the Employee model class Employee(models.Model): emp_id = models.CharField(max_length=10,primary_key=True) emp_type = models.CharField(max_length=50) name = models.CharField(max_length=100) epf_no …

Total answers: 1

Not showing bio tag in Django?

Not showing bio tag in Django? Question: I’m working on a blog page, and I’m creating page about authors but it doesn’t work to load bio (short description). I don’t know why it wasn’t working, please see code below: this is author_detail.html <p>Bio: {{ author.bio }}</p> this is views.py def author_detail(request, slug): user = User.objects.get(username=slug) …

Total answers: 1

I cannot post comment in Django, no error

I cannot post comment in Django, no error Question: I’m working on a Django blog and I’m stuck here… I want to post comment and when I do that it just refresh the page and nothing happens. I have no idea where I’m making a mistake, please see my code below: this is views.py def …

Total answers: 1

Writing a tuple search with Django ORM

Writing a tuple search with Django ORM Question: I’m trying to write a search based on tuples with the Django ORM syntax. The final sql statement should look something like: SELECT * FROM mytable WHERE (field_a,field_b) IN ((1,2),(3,4)); I know I can achieve this in django using the extra keyword: MyModel.objects.extra( where=["(field_a, field_b) IN %s"], …

Total answers: 3

Passing partial=True down to nested serializer in DRF

Passing partial=True down to nested serializer in DRF Question: I have two serializers organised like this: class OuterSerializer(): inner_obj = InnerSerializer(many=True, required=False) other fields …… class InnerSerializer(): field_1 = CharField() field_2 = CharField() Now my use case is to partial update the outer serializer’s model. How I’m doing that is: def partial_update(self, request, *args, **kwargs): …

Total answers: 1