django-queryset

Django template filter queryset

点点圈 提交于 2019-12-11 02:55:41
问题 I'm new in django. I has a django application where stores products categorized by 'X' and 'Y'. views.py ... class CartListView(ListView): template_name = 'checkout/list.html' context_object_name = 'product_list' def get_queryset(self): return Product.objects.filter(category__slug='X') | Product.objects.filter(category__slug='Y') def get_context_data(self, **kwargs): context = super(CartListView, self).get_context_data(**kwargs) context['minicurso'] = get_object_or_404(Category, slug='X')

complex annotate on django query set

只谈情不闲聊 提交于 2019-12-11 02:37:46
问题 I have an issue where I can't quite get all the information I need from a complex .annotate() call in my django view. Here's my model: RECORD_STATUS = ( (0, "Hidden"), (1, "Unhidden"), (2, "Deleted"), ) class Activity(models.Model): event_date = models.DateTimeField() duration = models.IntegerField(default=0) username = models.CharField(max_length=200) record_id = models.CharField(max_length=100) record_status = models.IntegerField(choices=RECORD_STATUS) record_quality = models.FloatField

Django ORM - .update(…) with an extra(…) and F(…)

守給你的承諾、 提交于 2019-12-11 02:13:08
问题 I want to do one sql query to update a lot of models in a Django site. I want to change one char column/field to be based on the id and some text, in MySQL (which this site is), I'd do that with "UPDATE table SET blah = 'prefix'||id||'suffix'" . My first attempt of doing this in Django was: Model.objects.update(blah='prefix'+F('id')+'suffix') But that tries to give MySQL a + , not a || operator. My next attempt was to use the .extra(…) like so: Model.objects.extra(select={'newvalue':'"prefix"

django - queryset order_by field that has characters and integers

时光怂恿深爱的人放手 提交于 2019-12-11 02:03:46
问题 I have a model that has a field (lets call it this_field ) which is stored as a string . The values in this_field are in the form Char### - as in, they have values such as: A4 or A34 or A55 (note that they will always have the same first character). Is there a way to select all the records and order them by this field? Currently, when I do an .order_by('this_field') I get the order like this: A1 A13 A2 A25 etc... What I want is: A1 A2 A13 A25 etc... Any best approach methods or solutions to

Django: How to merge two related querysets in Django 0.96?

你离开我真会死。 提交于 2019-12-11 01:43:47
问题 I would like to merge one querysets related objects with another querysets related objects. Some sample code to explain: ## Models # sample models to illustrate problem class PetShop(models.Model): id = models.AutoField(primary_key=True) shop_name = models.CharField(maxlength=255) cats = models.ManyToManyField(Cat) class Cat(models.Model): id = models.AutoField(primary_key=True) cat_name = models.CharField(maxlength=50, blank=True) ## View def MergePetsInShop(request): source_shop = PetShop

Annotate queryset with whether matching related object exists

萝らか妹 提交于 2019-12-11 00:47:27
问题 I have two models with an explicit many-to-many relationship: a thing, auth.user, and a "favorite" model connecting the two. I want to be able to order my "thing"s by whether or not they are favorited by a particular user. In Sqlite3, the best query i've come up with is (roughly) this: select *, max(u.name = "john cleese") as favorited from thing as t join favorite as f on f.thing_id = t.id join user as u on f.user_id = u.id group by t.id order by favorited desc ; The thing tripping me up in

Passing Django Queryset in Views to Template

左心房为你撑大大i 提交于 2019-12-10 23:28:01
问题 I have a Django Views which has some logic for passing the correct category to the template. class ProductListView(ListView): model = models.Product template_name = "catalogue/catalogue.html" def get_queryset(self): category = self.kwargs.get("category") if category: queryset = Product.objects.filter(category__iexact=category) else: queryset = Product.objects.all() return queryset I can't work out how to pass this to the template, my template code is as below: {% for product in products %}

Django ORM orderby exact / prominent match to be on top

点点圈 提交于 2019-12-10 23:15:50
问题 I need to order the results based on the length of match in Django ORM. I have a Suburb table with location details in name field. I have a requirement to search the table with given text and order by exact match / most prominent match to be the top For example: 1) if search string is 'America' then the result should be [America, South America, North America ..] in this case we found a complete match, which has to be the first element. 2) if search is port then the result should be ['Port

Django ManyToMany filtering by set size or member in the set

偶尔善良 提交于 2019-12-10 19:50:42
问题 I am using django to maintain a database of messages. Among others I have the following models : class User(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField(max_length=10) class Message(models.Model): id = models.IntegerField(primary_key=True) body = models.CharField(max_length=200) users = models.ManyToManyField(User) I am trying to write a utility method that for a given user gives me the messages he (and he alone) is associated with. i.e. for: m1 = Message

Is there a way to check whether a related object is already fetched?

 ̄綄美尐妖づ 提交于 2019-12-10 19:04:34
问题 I would like to be able to check if a related object has already been fetched by using either select_related or prefetch_related , so that I can serialize the data accordingly. Here is an example: class Address(models.Model): street = models.CharField(max_length=100) zip = models.CharField(max_length=10) class Person(models.Model): name = models.CharField(max_length=20) address = models.ForeignKey(Address) def serialize_address(address): return { "id": address.id, "street": address.street,