django-queryset

django - convert a list back to a queryset [duplicate]

拜拜、爱过 提交于 2019-12-27 17:09:13
问题 This question already has answers here : A QuerySet by aggregate field value (3 answers) Closed 5 years ago . I have a handful of records that I would like to sort based on a computed value. Got the answer over here... like so: sorted(Profile.objects.all(), key=lambda p: p.reputation) on a Profile class like this: class Profile(models.Model): ... @property def reputation(self): ... Unfortunately the generic view is expecting a queryset object and throws an error if I give it a list. Is there

Querying data from Django

二次信任 提交于 2019-12-25 14:49:41
问题 Here's what my model structure looks like: class Visitor(models.Model): id = models.AutoField(primary_key=True) class Session(models.Model): id = models.AutoField(primary_key=True) visit = models.ForeignKey(Visitor) sequence_no = models.IntegerField(null=False) class Track(models.Model): id = models.AutoField(primary_key=True) session = models.ForeignKey(Session) action = models.ForeignKey(Action) when = models.DateTimeField(null=False, auto_now_add=True) sequence_no = models.IntegerField

Querying data from Django

不想你离开。 提交于 2019-12-25 14:49:11
问题 Here's what my model structure looks like: class Visitor(models.Model): id = models.AutoField(primary_key=True) class Session(models.Model): id = models.AutoField(primary_key=True) visit = models.ForeignKey(Visitor) sequence_no = models.IntegerField(null=False) class Track(models.Model): id = models.AutoField(primary_key=True) session = models.ForeignKey(Session) action = models.ForeignKey(Action) when = models.DateTimeField(null=False, auto_now_add=True) sequence_no = models.IntegerField

Django merge 2 querysets in staggered/alternating fashion without duplicates?

一曲冷凌霜 提交于 2019-12-25 08:46:36
问题 Follow up question to this one: Django merge 2 querysets in staggered/alternating fashion? I'm wondering is there an efficient way to avoid duplicates in the result? i.e. the generators will only return items we have not seen before. 来源: https://stackoverflow.com/questions/45834009/django-merge-2-querysets-in-staggered-alternating-fashion-without-duplicates

Django - get all objects that don't belong to M2M

女生的网名这么多〃 提交于 2019-12-25 07:50:21
问题 I have a model with field: class Product(models.Model): subproducts = models.ManyToManyField("self", blank=True) I need to overwrite admin's field queryset, to display only that objects that don't belong to any m2m relation. I have no idea how to get them. So if I have: product1, product2, product3, product4. product1 contains in subproducts: product2 I need a query that will get, in that situation, product3 and product4 Any idea how to get that? 回答1: I think that did the trick: Product

get a multiple choice queryset in Django view and save it

久未见 提交于 2019-12-25 03:12:08
问题 I have a multiple choice field with a foreign key. I want to save which keeper was attending a training session and I want to list all keepers as a multiple choice field. class AddAttendance(forms.ModelForm): attendanceKeeper = Attendance.objects.only("keeper","present").all() keeperValues = Attendance.objects.values_list("keeper__id", flat=True).distinct() keeper = forms.ModelMultipleChoiceField(widget=forms.widgets.CheckboxSelectMultiple, queryset=Keeper.objects.filter(id__in=keeperValues,

Django distinct() not returning distinct values

瘦欲@ 提交于 2019-12-25 03:06:24
问题 I have a Session model like this: class Session(models.Model): user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions") flavor = models.ForeignKey(Flavor, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions") .... And I'm trying to run a query: sessions = Session.objects.all().values('flavor__pk', 'user__pk').distinct() But when I then print the sessions object I get this: <QuerySet [{'user__pk': 14544, 'flavor__pk': 1}, {

Loop over related model's children in Django template

自作多情 提交于 2019-12-25 01:37:59
问题 I have a model for a company. Then I have a base model for company posts. It contains common posts attributes. An attribute is the company that publishes the posts. It refers to the Company model with a ForeignKey. Finally I have a child model (based on the CompanyPost base model) for posts of type A: class Company(models.Model): name = models.CharField(...) ... class CompanyPost(models.Model): company = models.ForeignKey(Company,...) ... class PostA(CompanyPost): name = ... In a template I

Django qs1.difference(qs2, qs3) not supported by backend

好久不见. 提交于 2019-12-25 01:08:47
问题 I need to check a table from a remote db and update another table in my db. The models have same fields but checking with difference method will return an error: qs1 get the data from one postgresql db qs2 get the data from one mysql db qs1 = (Local.objects .values_list('ref') ) qs2 = (Remote.objects .filter() .values_list('ref')) >>> qs1.difference(qs1, qs2) DatabaseError: intersection not supported on this database backend. 回答1: I'm not exactly sure what you think your code is doind. qs1

How to latest objects created in django models by similar field value?

旧时模样 提交于 2019-12-25 00:11:02
问题 I have created an app here people can upload 'csv' files of their items details to the server, and then perform various opertaions upon it. But, because every item has it's own unique ID, whenever a csv is uploaded, there is no way to figure out which item-batch was uploaded last. Upload function This is my function in views.py importing the data in the Itembatch model: @method_decorator([login_required, teacher_required], name='dispatch') class UploadedItems(ListView): model = ItemBatch