django-queryset

ViewSet class variable

ViewSet class variable Question: Now I have the following logic implemented for a GET request in Django Rest Framework: class SomeViewSet(mixins.ListModelMixin, GenericViewSet): count = None def get_queryset(self): query_set = … # some_logic self.count = query_set.count() return query_set def list(self, request, *args, **kwargs): response = super().list(request, *args, **kwargs) response.data = {‘count’: self.count, ‘data’: response.data} return response …

Total answers: 1

Django ForaignKey attribute not showing up in html

Django ForeignKey attribute is not showing up in HTML Question: I have some models and connected each other. And in my views I get the Chapter model ordered by date published, and then remove the ones with same Manga (foreign key associated with other model). But apparently accept for the fields is not connected with …

Total answers: 2

Showing two model in same page Django

Showing two model in same page Django Question: I have two models: class Post(models.Model): title= models.CharField(max_length=255) author = models.ForeignKey(User, on_delete=models.CASCADE) body = models.TextField() postimage = models.ImageField(null= True, blank= True, upload_to="images/") created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now() self.save() def __str__(self): return self.title + " | "+ str(self.author) def get_absolute_url(self): return …

Total answers: 1

Django execute previous action on refresh

Django execute previous action on refresh Question: I have tried to add a book in to the database using an HTML form. After the submission, the page redirect to a page where all the books are listed .Then whenever I refresh the page , the data is became duplicated. How do I resolve this problem? …

Total answers: 1

Django form with m2m relationship not saving

Django form with m2m relationship not saving Question: I have a form where I want request.user to populate as little as possible and rely on the views to populate other fields automatically. As a result, some of these fields are not rendered on the form. The code in my view seems to work fine for …

Total answers: 1

Django – Problem with Model Manager – Query

Django – Problem with Model Manager – Query Question: I’m still a beginner and I’m stuck in a challenging spot for me. I can’t get data from a foreign key table to be inserted "correctly" into a column of a ListView. I basically want to create a list view of a table (FeatureFilm). This also …

Total answers: 1

How to compare list of models with queryset in django?

How to compare list of models with queryset in django? Question: I have a serializer: class MySerializer(serializers.ModelSerializer): class Meta: model = models.MyClass fields = "__all__" def validate(self, data): user = self.context.get("request").user users = data.get("users") users_list = User.objects.filter(organization=user.organization) return data users will print a list of models like this: [<User: User 1>, <User: User 2>] users_list …

Total answers: 3

How to get data from both sides of a many to many join in django

How to get data from both sides of a many to many join in django Question: Let’s say I have the following models: class Well(TimeStampMixin, models.Model): plate = models.ForeignKey(Plate, on_delete=models.CASCADE, related_name="wells") row = models.TextField(null=False) column = models.TextField(null=False) class Meta: unique_together = [["plate", "row", "column"]] class Antibiotic(TimeStampMixin, models.Model): name = models.TextField(null=True, default=None) class WellConditionAntibiotic(TimeStampMixin, models.Model): wells …

Total answers: 1

Django queryset related field lookup with filtering the last object

Django queryset related field lookup with filtering the last object Question: I am building a Price comparing django app, i came across this scenario where i need to filter the Last price for each seller in a related field lookup. Seller model : class Seller(models.Model): name = models.CharField(max_length=250, null=True) Part model : class Part(models.Model): name …

Total answers: 2