queryset

Modifying an attribute for each object in a queryset

Hi I've been using Django for over a year, but I think I've missed out on some very fundamental thing. I have a rather large queryset (1000+ objects) and I'd like to change a single attribute for each of the objects in that queryset. Is this really the way to go? I'm sure there is something simpler? for obj in qs: obj.my_attr = True ...

Howto merge 2 Django QuerySets in one and make a SELECT DISTINCT

Hi, thats my code: ##### models.py ##### class SinglePoint(models.Model): attributes = models.TextField(blank=True) name = models.CharField(max_length=100) geom = models.PointField() #Kartenposition objects = models.GeoManager() class Connection(models.Model): name = models.CharField(max_length=100) #points = m...

How Do I Select all Objects via a Relationship Model

Given the Model: class Profile(models.Model): user = models.ForeignKey(User, unique=True) class Thingie(models.Model): children = models.ManyToManyField('self', blank=True, symmetrical=False) class Relation(models.Model): profile = models.ForeignKey(Profile) thingie = models.ForeignKey(Thingie) How would one return ...

Newbie: Django : Adding calculated results to Queryset before passing to template

Hi All Its day two of my new life with Django, please excuse the simplicity of my question. I have an existing DB table(read-only access) that I have successfully displayed the contents of on a webpage using urls, views, models and all that good stuff. The challenge I have is the table does not contain all the information I need to di...

Django ORM: Chaining aggregated querysets into one

Can I chain these two querysets into one? qs1 = OrderTicket.objects.filter(date__gt=datetime.date(2009, 1, 1), date__lt=datetime.date(2009, 1, 30)).values('order_type').annotate(value_1 = Sum('gbp_value')).order_by('order_type'), qs2 = OrderTicket.objects.filter(date__gt=datetime.date(2009, 2, 1), date__lt=datetime.date(2009, 2, 30)).va...

Following a foreign-key of a commented item in a Django Queryset

Dear all, I'm working on a Django application allowing one to comment either a Text, its Paragraphs or the comments themselves. I'm using the Comment app bundled with Django, and A Text class instance is made up of Inline Paragraph class instances. I'm desperately looking for a way get back a QuerySet/List of all the Comments related d...

Django: Extending Querysets / Connect multiple filters with OR

Hi! I have to work with a queryset, that is already filtered, eg. qs = queryset.filter(language='de') but in some further operation i need to undo some of the already applied filtering, eg not to take only the rows with language='de' but entries in all languages. Is there a way to apply filter again and have the new parameters connected ...

Django: Printing a queryset dynamically in the template

How can I print the columns specified in "fields_i_want" instead of hardcoding the column names in the template code? # Let's say I have this in my view: foo = Foo.objects.filter(some_field='bar').select('field1', 'field2', 'field3') fields_i_want = ['field1', 'field2'] # Then in the template I want to do something like this: <TABLE id...

Django Admin: Getting a QuerySet filtered according to GET string, exactly as seen in the change list?

Hi all In the Django admin, the user can set filters which limit the rows displayed in the change list. How can I get a QuerySet instance with filters set as defined by the query string? For instance, if I pass ?start_date_gte=2009-11-06, the Django admin will apply a qs.filter(start_date__gte...) somewhere. How can I access such a Quer...

How can I use multiple queryset in a ModelChoiceField?

I'm just beginning to learn Django and I would like to use different queryset in a ModelChoiceField. I have 3 models like that : class Politic(models.Model): name = models.CharField(max_length=100) class Economic(models.Model): name = models.CharField(max_length=100) class Category(models.Model): politic = models.ForeignKey(Politic,...

Modifying QuerySet result

Is it possible to change some specific items in a QuerySet object? In my case i'm trying to slicing "title" fields with length more than 40 characters and append "..." at the end of field. ...

store a queryset in the session with django

I have problem storing a big queryset in the session. This queryset is from a search and I need to store it for paginate inside every result. This is the code in my view: c = queryset.order_by('-order') request.session['query_search'] = c You can see an example in my site: http://www.lukmi.com/classifieds/chicas/escorts/barcelona/ Th...

Query and paginate three types of models at the same time in django

Hi, In django I have three models: SimpleProduct ConfigurableProduct Instead of showing several variations of SimpleProducts, the user will see one product with options like color. GroupProduct - Several SimpleProducts that are sold together. First I'm creating all the SimpleProducts, then I create ConfigurableProducts from several ...

Retrieving unique results in Django queryset based on column contents

I am not sure if the title makes any sense but here is the question. Context: I want to keep track of which students enter and leave a classroom, so that at any given time I can know who is inside the classroom. I also want to keep track, for example, how many times a student has entered the classroom. This is a hypothetical example tha...

Creating custom Field Lookups in Django

How do you create custom field lookups in Django? When filtering querysets, django provides a set of lookups that you can use: __contains, __iexact, __in, and so forth. I want to be able to provide a new lookup for my manager, so for instance, someone could say: twentysomethings = Person.objects.filter(age__within5=25) and get back...

How to limit queryset/the records to view in Django admin site?

By default Django admin site shows all records of a related model/table for viewing. How can I show only the records that meet certain criteria? ...

Is there any way to do a case-insensitive IN query in Django?

Nearly every kind of lookup in Django has a case-insensitive version, EXCEPT in, it appears. This is a problem because sometimes I need to do a lookup where I am certain the case will be incorrect. Products.objects.filter(code__in=[user_entered_data_as_list]) Is there anything I can do to deal with this? Have people come up with a ha...

Django, relational querysets.

How do I express this SQL query in a Django Queryset? SELECT * FROM Table1, Table2 WHERE Table1.id_table2 = Table2.id_table2; Be aware that the structure of table1 implyes a id_table2 foreign key... Why? Because I want to replace the id_table2 in the Table1 table1.object.all() listing with values asociated to the register involved in...

Django: problem with merging querysets after annotation

Hi I have a manager for "Dialog" looking like this: class AnnotationManager(models.Manager): def get_query_set(self): return super(AnnotationManager, self).get_query_set().annotate( num_votes=Count('vote', distinct=True), num_comments=Count('comment', distinct=True), num_commentators = Count('comment__user',...

Django Querying Question

If I were to have two different QuerySets in Django, both representing a ManyToMany relation with the same model, how would I find the intersections? ...