django-queryset

Django: Generating a queryset from a GET request

安稳与你 提交于 2019-12-12 09:43:35
问题 I have a Django form setup using GET method. Each value corresponds to attributes of a Django model. What would be the most elegant way to generate the query? Currently this is what I do in the view: def search_items(request): if 'search_name' in request.GET: query_attributes = {} query_attributes['color'] = request.GET.get('color', '') if not query_attributes['color']: del query_attributes['color'] query_attributes['shape'] = request.GET.get('shape', '') if not query_attributes['shape']: del

comparing querysets in django TestCase

无人久伴 提交于 2019-12-12 09:29:41
问题 I have a very simple view as follows def simple_view(request): documents = request.user.document_set.all() return render(request, 'simple.html', {'documents': documents}) To test the above view in my test case i have the following method which errors out. Class SomeTestCase(TestCase): # ... def test_simple_view(self): # ... some other checks docset = self.resonse.context['documents'] self.assertTrue(self.user.document_set.all() == docset) # This line raises an error # ... The error i get is

Admin action- queryset to apply Many to Many

最后都变了- 提交于 2019-12-12 03:25:23
问题 How do I use the admin action to create a queryset which will apply a Many-to-Many value? I understand the 'value' will have to already exist (in my case, the colour itsel will have to exist). Models class Colours(models.Model): colour_name = models.CharField(max_length=50) class Car(models.Model): brand = models.CharField(max_length=200) available_colours = models.ManyToManyField(Colours, blank=True) Admin.py class CarAdmin(admin.ModelAdmin): actions = ['Red'] Attempt 1: only works for FK

Django, which function belongs to QuerySet and Manager?

╄→гoц情女王★ 提交于 2019-12-12 03:24:46
问题 I used to think QuerySet method return QuerySet instances, but it apparently not. For instance, count() is a queryset method not manager's How do I decide which functions go to custom QuerySet and which go to custom Manager class? 回答1: It makes sense to be able to access some functions like count() on the manager and the queryset. This allows you to do: Blog.objects.count() # total number of blogs Blog.objects.filter(status='PUBLISHED').count() # Number of published blogs Django has a method

How can I select one object per each related object in django?

佐手、 提交于 2019-12-12 02:22:21
问题 If I have two classes: class Group(models.Model): name = models.CharField(...) class Item(models.Model): group = models.ForeignKey(Group) published = models.DateTimeField(auto_now_add=True) How can I make a QuerySet to select the latest published Item from each Group? I guess it should be something like Item.objects.order_by('published').distinct('group') but I can't make it work. 回答1: models.py class Group(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return

django self join query using aliases

萝らか妹 提交于 2019-12-12 02:08:47
问题 am trying to use queryset to perform the following query without using raw SQL. any idea how can do that? select * from category_main a, category_list b, category_main c where b.main_id=c.id and a.id=c.parent_id UPDATED below are my models class Main(models.Model): slug = models.SlugField() is_active = models.BooleanField(default=True) site = models.ForeignKey(Site) parent = models.ForeignKey('self', blank=True, null=True, limit_choices_to={'parent' : None}) class Meta: unique_together = ((

Need to annotate Django querySet based on which Q object was found

我的梦境 提交于 2019-12-12 01:52:51
问题 So I have a query with a few Q objects that are OR-ed together (to achieve a UNION), and I want to annotate each result with which Q object was a match. This is so when I go to display my query results, I can highlight which search term(s) were hits on each result. Here's the code that produces the resulting querySet: Gene.objects.filter(Q(EC__EC='3.2.1.4')|Q(Protein_Family__name__in=famList)|Q(Pfam__Pfam__in=pfams),Protein_length__gte=100, Distance_From_Contig_Upstream__gte=10, Distance_From

Custom delete method on queryset

谁说胖子不能爱 提交于 2019-12-12 01:48:51
问题 My Django model has custom logic in the delete method. Therefore since I wane to make sure that this logic is called when I call delete on my queryset, I wrote my own queryset delete. class MyQuerySet(QuerySet): # Do we have to be any fancier here? def delete(self): for m in self: m.delete() and my question is do I have to do anything fancier than iterating and calling delete on each instance? 回答1: You should clear the result cache so if the queryset will be reused then DB query will be

object_detail() got multiple values for keyword argument 'queryset' while inputting only one

自闭症网瘾萝莉.ら 提交于 2019-12-12 01:27:57
问题 from django.conf.urls.defaults import * from django.conf import settings from Website.Blog.models import Post # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() index = { 'queryset': Post.objects.all(), 'date_field': 'created_on', 'template_name': 'index.html', 'num_latest': 5 } post = { 'template_name': 'index.html', 'queryset': Post.objects.all(), # only here, what could be wrong? 'slug': 'slug', } urlpatterns = patterns('', # Example:

Django - OperationalError: (1054, “Unknown column 'xx' in 'field list'”)

☆樱花仙子☆ 提交于 2019-12-11 21:33:58
问题 I have a problem with a simple insert in my database (as explained in this post). I'm asking a new question as the issue is a bit different. My model and sql table are the same, I try to insert a row with one field specified and it returns an error saying that it doesn't know another field .. My sql table looks like this : FeuilleTemps | CREATE TABLE `FeuilleTemps` ( `f_id` int(11) NOT NULL AUTO_INCREMENT, `f_id_user` int(11) DEFAULT NULL, `f_date` date DEFAULT NULL, `f_id_proj` int(11)