I'm struggling getting my head around the Django's ORM. What I want to do is get a list of distinct values within a field on my table .... the equivalent of one of the following:
SELECT DISTINCT myfieldname FROM mytable
(or alternatively)
SELECT myfieldname FROM mytable GROUP BY myfieldname
I'd at least like to do it the Django way...
Hope this makes sense...
Is there a simple way to return a set of values from a table based on a single column's values being distinctly unique? What I'm hoping for is something like:
SegCode.query.filter(ref.unique()).only('ref')
That's not real code, but I was hoping there was some simple function out there that will accomplish thi...
Hello All:
I am hoping someone can help me out with a quick question I have regarding chaining Django querysets. I am noticing a slow down because I am evaluating many data points in the database to create data trends. I was wondering if there was a way to have the chained filters evaluated locally instead of hitting the database. He...
Like in this question, except I want to be able to have querysets that return a mixed body of objects:
>>> Product.objects.all()
[<SimpleProduct: ...>, <OtherProduct: ...>, <BlueProduct: ...>, ...]
I figured out that I can't just set Product.Meta.abstract to true or otherwise just OR together querysets of differing objects. Fine, but...
There are database string values that are sometimes stored with unnecessary spaces:
"SDF@#$#@ 132423"
Given the value without spaces in the UI of a program:
"SDF@#$#@132423"
How could I do a Django queryset filter to find the Database value (with spaces) from the UI input value sans spaces?
...
I cannot run ['abc'].append( MyModel.objects.all() ) since it generates exception 'NoneType' object is not iterable if MyModel has no entry.
any workaround or something like ? : in c++
edit:
my statement is actually
','.join([ str(e) for e in ['abc','def'].append( MyModel.objects.all() ) ])
it seems that the problem is caused by ...
This is just an example, but given the following model:
class Foo(models.model):
bar = models.IntegerField()
def __str__(self):
return str(self.bar)
def __unicode__(self):
return str(self.bar)
And the following QuerySet object:
foobar = Foo.objects.filter(bar__lt=20).distinct()
(meaning, a set of uniq...
Let's say I have a site where Users can add Entries through admin panel. Each User has his own Category he is responsible for (each Category has an Editor assigned through ForeingKey/ManyToManyField).
When User adds Entry, I limit the choices by using EntryAdmin like this:
class EntryAdmin(admin.ModelAdmin):
(...)
def formfiel...
This question is connected to my other question but I changed the logic a bit.
I have models like this:
from django.contrib.auth.models import Group
class Category(models.Model):
(...)
editors = ForeignKey(Group)
class Entry(models.Model):
(...)
category = ForeignKey(Category)
Now let's say User logs into admin panel an...
I have model with a Foreign Key to itself like this:
class Concept(models.Model):
name = models.CharField(max_length=200)
category = models.ForeignKey('self')
But I can't figure out how I can select all concepts that have nonzero children value. Is this possible with django QuerySet API or I must write custom SQL?
...
Hi, I have a list of names that I want to match case insensitive, is there a way to do it without using a loop like below?
a = ['name1', 'name2', 'name3']
result = any([Name.objects.filter(name__iexact=name) for name in a])
...
I'm using the extra() modifier in a view.
(See http://docs.djangoproject.com/en/1.1/ref/models/querysets/#extra-select-none-where-none-params-none-tables-none-order-by-none-select-params-none )
Here's the the code:
def viewname(request)
...
exact_matchstrings=[]
exact_matchstrings.append("(accountprofile.first_name LIKE '"...
I need to amend QuerySet data when i return it to a template.
for example, model.objects.all() returns a date (with other fields), but i also want to return the number of days since that date has passed. This is so that in the template, i can say "you last logged in 4 days ago".
What is the best way to do this?
...
In Django 1.1 I was able to produce the SQL used by a QuerySet with this notation:
QuerySet.query.as_sql()
In Django 1.2, this raises as AttributeError.
Anyone know the Django 1.2 equivalent of that method?
Thanks
...
I need order a Queryset by date in desc order, but i need put in the end the objects at the end, I do this:
qs1 = Model.objects.exclude(date=None).order_by('-date')
qs2 = Model.objects.filter(date=None).order_by('-date')
and my list is:
l = list(qs1)+list(qs2)
There is a more efficiently way for this?
...
How can i use order_by like order_by('field1'*'field2')
For example i have items with price listed in different currencies, so to order items - i have to make currency conversion.
class Currency(models.Model):
code = models.CharField(max_length=3, primary_key=True)
rateToUSD = models.DecimalField(max_digits=20,decimal_pla...
Is there a simple way to discard/remove the last result in a queryset without affecting the db?
I am trying to paginate results in Django, but don't know the total number of objects for a given query.
I was planning on using next/previous or older/newer links, so I only need to know if this is the first and/or last page.
First is easy...
Hi,
I am using the filter icontains to search for words but I only want it to match whole words. e.g. if I searched for liver I wouldn't want it returning delivery.
my query looks like this
MyModel.objects.filter(title__icontains=search_word)
I have seen the filter __search but this does not bring back results with 3 characters or l...
Hi folks,
I'm finding this a bit tricky! Maybe someone can help me on this one
I have the following model:
class Unicorn(models.Model):
horn_length = models.IntegerField()
skin_color = models.CharField()
average_speed = models.IntegerField()
magical = models.BooleanField()
affinity = models.CharField()
I would like to sea...
Hi folks,
is there any way of doing the following
Unicorn.objects.or_filter(magical=True).or_filter(unicorn_length=15).or_filter(skin_color='White').or_filter(skin_color='Blue')
where or_filter stands for an isolated match
I remember using something similar but cannot find the function anymore!
Help would be great! Thanks :)
...