django-orm

Django order_by specific order

送分小仙女□ 提交于 2019-11-27 14:01:32
问题 Is it possible to replicate this kind of specific sql ordering in the django ORM: order by (case when id = 5 then 1 when id = 2 then 2 when id = 3 then 3 when id = 1 then 4 when id = 4 then 5 end) asc ? 回答1: You could do it w/ extra() or more plain raw() , but they can not work well w/ more complex situation. qs.extra(select={'o':'(case when id=5 then 1 when id=2 then 2 when id=3 then 3 when id=1 then 4 when id=4 then 5 end)', order_by='o'} YourModel.raw('select ... order by (case ...)') For

Django Admin ManyToManyField

只愿长相守 提交于 2019-11-27 13:08:33
问题 I've made a model (models.py): class opetest(models.Model): name = models.CharField(max_length=200) author = models.ForeignKey(User, related_name='author') description = models.TextField(u'Test description', help_text = u'Some words about quiz') pub_date = models.DateTimeField('date published', blank=False) vacancies = models.ManyToManyField(Vacancy, blank=True) students = models.ManyToManyField(User, blank=True, related_name='opetests') #This field I want to edit on "User change page"

django-orm case-insensitive order by

妖精的绣舞 提交于 2019-11-27 12:38:50
I know, I can run a case insensitive search from DJango ORM. Like, User.objects.filter(first_name__contains="jake") User.objects.filter(first_name__contains="sulley") User.objects.filter(first_name__icontains="Jake") User.objects.filter(first_name__icontains="Sulley") And also, I can fetch them as user_list = User.objects.all().order_by("first_name") # sequence: (Jake, Sulley, jake, sulley) user_list = User.objects.all().order_by("-first_name") # for reverse # sequence: (sulley, jake, Sulley, Jake) Is there a direct way for a case-insensitive fetch?? As in I want a sequence as # desired

LEFT JOIN Django ORM

做~自己de王妃 提交于 2019-11-27 12:28:30
I have the following models: class Volunteer(models.Model): first_name = models.CharField(max_length=50L) last_name = models.CharField(max_length=50L) email = models.CharField(max_length=50L) gender = models.CharField(max_length=1, choices=GENDER_CHOICES) class Department(models.Model): name = models.CharField(max_length=50L, unique=True) overseer = models.ForeignKey(Volunteer, blank=True, null=True) location = models.CharField(max_length=100L, null=True) class DepartmentVolunteer(models.Model): volunteer = models.ForeignKey(Volunteer) department = models.ForeignKey(Department) assistant =

Saving Many To Many data via a modelform in Django

≡放荡痞女 提交于 2019-11-27 11:56:38
I have a problem saving many to many fields from a form. Here are my models: class TextIssue(models.Model): Issue = models.CharField(max_length=150, unique=True) def __unicode__(self): return self.Issue class PadIssue(models.Model): Issue = models.CharField(max_length=150, unique=True) def __unicode__(self): return self.Issue class PHIssue(models.Model): Data = models.ForeignKey(Data) TextIssue = models.ManyToManyField(TextIssue, blank=True, null=True) PadIssue = models.ManyToManyField(PadIssue, blank=True, null=True) Notes = models.TextField() def clean(self): from django.core.exceptions

Django Blob Model Field

可紊 提交于 2019-11-27 11:34:54
How do you store a "blob" of binary data using Django's ORM, with a PostgreSQL backend? Yes, I know Django frowns upon that sort of thing, and yes, I know they prefer you use the ImageField or FileField for that, but suffice it to say, that's impractical for my application. I've tried hacking it by using a TextField, but I get occassional errors when my binary data doesn't strictly confirm to the models encoding type, which is unicode by default. e.g. psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0xe22665 Spacedman This snippet any good: http://djangosnippets.org/snippets/1597

Django, query filtering from model method

怎甘沉沦 提交于 2019-11-27 11:02:42
问题 I have these models: def Foo(Models.model): size = models.IntegerField() # other fields def is_active(self): if check_condition: return True else: return False def Bar(Models.model): foo = models.ForeignKey("Foo") # other fields Now I want to query Bars that are having active Foo's as such: Bar.objects.filter(foo.is_active()) I am getting error such as SyntaxError at / ('non-keyword arg after keyword arg' How can I achieve this? 回答1: You cannot query against model methods or properties.

Django select only rows with duplicate field values

久未见 提交于 2019-11-27 10:28:56
suppose we have a model in django defined as follows: class Literal: name = models.CharField(...) ... Name field is not unique, and thus can have duplicate values. I need to accomplish the following task: Select all rows from the model that have at least one duplicate value of the name field. I know how to do it using plain SQL (may be not the best solution): select * from literal where name IN ( select name from literal group by name having count((name)) > 1 ); So, is it possible to select this using django ORM? Or better SQL solution? Chris Pratt Try: from django.db.models import Count

Select distinct values from a table field

我是研究僧i 提交于 2019-11-27 09:17:10
问题 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 before resorting to raw sql. For example, with a table: id, street, city 1, Main Street, Hull 2, Other Street, Hull 3, Bibble Way, Leicester 4, Another Way,

Django: ManyToMany filter matching on ALL items in a list

放肆的年华 提交于 2019-11-27 09:03:19
I have such a Book model: class Book(models.Model): authors = models.ManyToManyField(Author, ...) ... In short: I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single query that does it, but any suggestions will be helpful. In long: Here is what I tried, (that failed to run getting an AttributeError) # A sample set of authors target_authors = set((author_1, author_2)) # To reduce the search space, # first retrieve those books with just 2 authors. candidate_books = Book.objects.annotate(c=Count('authors')).filter(c=len(target