django-orm

Select DISTINCT individual columns in django?

孤者浪人 提交于 2019-11-27 00:24:25
I'm curious if there's any way to do a query in Django that's not a " SELECT * FROM... " underneath. I'm trying to do a " SELECT DISTINCT columnName FROM ... " instead. Specifically I have a model that looks like: class ProductOrder(models.Model): Product = models.CharField(max_length=20, promary_key=True) Category = models.CharField(max_length=30) Rank = models.IntegerField() where the Rank is a rank within a Category . I'd like to be able to iterate over all the Categories doing some operation on each rank within that category. I'd like to first get a list of all the categories in the system

Creating Partial Indexes with Django 1.7

≯℡__Kan透↙ 提交于 2019-11-26 23:14:54
问题 The documentation for Django 1.7 mentions RunSQL classes can be used to create partial indexes on your tables. I have a table where I want the combination of title , blog & category to be unique. However if category is not provided, the combination of title & blog should still be unique. class Post(models.Model): title = models.CharField(max_length=200) blog = models.ForeignKey(Blog) category = models.ForeignKey(Category, null=True, blank=True) I can achieve this constraint with partial

Direct assignment to the forward side of a many-to-many set is prohibited. Use emails_for_help.set() instead

时光怂恿深爱的人放手 提交于 2019-11-26 22:51:44
问题 I am new to Django and didn't find any reference regarding this issue. I am getting this error when i use many to many field in django model(models.py). i guess the issue is assining m2m filed in view(views.py) from form(forms.py). How to assign m2m field in view. Django version 2.O python 3.5 models.py class User(AbstractUser): username=models.CharField(max_length=20) email = models.EmailField(_('email address'), unique=True) class Setupuser(models.Model): organization=models.CharField(max

Django values_list vs values

泪湿孤枕 提交于 2019-11-26 22:31:49
问题 In Django, what's the difference between the following two: Article.objects.values_list('comment_id', flat=True).distinct() vs Article.objects.values('comment_id').distinct() My goal is to get a list of unique comment ids under each Article . I've read the documentation (and in fact have used both approaches). The results overtly seem similar. 回答1: The values() method returns a QuerySet containing dictionaries: <QuerySet [{'comment_id': 1}, {'comment_id': 2}]> The values_list() method returns

How to create an object for a Django model with a many to many field?

此生再无相见时 提交于 2019-11-26 21:22:10
My model: class Sample(models.Model): users = models.ManyToManyField(User) I want to save both user1 and user2 in that model: user1 = User.objects.get(pk=1) user2 = User.objects.get(pk=2) sample_object = Sample(users=user1, users=user2) sample_object.save() I know that's wrong, but I'm sure you get what I want to do. How would you do it ? Daniel Hepper You cannot create m2m relations from unsaved objects. If you have the pk s, try this: sample_object = Sample() sample_object.save() sample_object.users.add(1,2) Update: After reading the saverio's answer , I decided to investigate the issue a

Annotate with value of latest related in Django 1.8 using conditional annotation

本小妞迷上赌 提交于 2019-11-26 20:48:46
问题 I have the following models: class City(models.Model): ... class Census(models.Model): city = models.ForeignKey(City) date = models.DateTimeField() value = models.BigIntegerField() Now I'd like to annotate a City-queryset with the value of the latest Census. How do I achieve that? I have tried: City.objects.annotate(population=Max('census__date')) # --> annotates date and not value City.objects.annotate(population=Max('census__value')) # --> annotates highest value, not latest City.objects

How to execute a GROUP BY … COUNT or SUM in Django ORM?

别等时光非礼了梦想. 提交于 2019-11-26 20:29:23
问题 Prologue: This is a question arising often in SO: Django Models Group By Django equivalent for count and group by How to query as GROUP BY in django? How to use the ORM for the equivalent of a SQL count, group and join query? I have composed an example on SO Documentation but since the Documentation will get shut down on August 8, 2017, I will follow the suggestion of this widely upvoted and discussed meta answer and transform my example to a self-answered post. Of course, I would be more

Serializing Foreign Key objects in Django

北战南征 提交于 2019-11-26 20:05:25
I have been working on developing some RESTful Services in Django to be used with both Flash and Android apps. Developing the services interface has been quite simple, but I have been running into an issue with serializing objects that have foreign key and many to many relationships. I have a model like this: class Artifact( models.Model ): name = models.CharField( max_length = 255 ) year_of_origin = models.IntegerField( max_length = 4, blank = True, null = True ) object_type = models.ForeignKey( ObjectType, blank = True, null = True ) individual = models.ForeignKey( Individual, blank = True,

How to use custom manager with related objects?

China☆狼群 提交于 2019-11-26 19:56:42
问题 I have a custom manager. I want to use it for related objects. I found use_for_related_fields in docs. But it does not work the way I used it: class RandomQueryset(models.query.QuerySet): def randomize(self): count = self.count() random_index = random.randint(0, count - 1) return self.all()[random_index] class RandomManager(models.Manager): use_for_related_fields = True def get_query_set(self): return RandomQueryset(self.model, using=self._db) def randomize(self): return self.get_query_set()

Chaining multiple filter() in Django, is this a bug?

人盡茶涼 提交于 2019-11-26 19:30:26
I always assumed that chaining multiple filter() calls in Django was always the same as collecting them in a single call. # Equivalent Model.objects.filter(foo=1).filter(bar=2) Model.objects.filter(foo=1,bar=2) but I have run across a complicated queryset in my code where this is not the case class Inventory(models.Model): book = models.ForeignKey(Book) class Profile(models.Model): user = models.OneToOneField(auth.models.User) vacation = models.BooleanField() country = models.CharField(max_length=30) # Not Equivalent! Book.objects.filter(inventory__user__profile__vacation=False).filter