django-orm

Django Many to Many Relationship Add Not Working

时光总嘲笑我的痴心妄想 提交于 2019-12-03 21:40:27
I'm using Django's ManyToManyField for one of my models. class Requirement(models.Model): name = models.CharField(max_length=200) class Course(models.Model): requirements = models.ManyToManyField(Requirement) I want to be able to assign requirements for my classes, so to do that, I try the following: I get a class, course, that is already saved or that I have just saved, and I run the following: c = Course.objects.get(title="STACK 100") req = Requirement.objects.get(name="XYZ") c.requirements.add(req) While this works when I do it through the Django manage.py shell, it does not work when I do

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

纵饮孤独 提交于 2019-12-03 17:30:36
问题 An example Many-to-Many through relationship in Django: class First(models.Model): seconds = models.ManyToManyField(Second, through='Middle') class Middle(models.Model): first = models.ForeignKey(First) second = models.ForeignKey(Second) class Second(models.Model): Following the documentation on intermediary models, only one model of the pair to be related contains the ManytoManyField , model First in the example above. Is this correct? If so, which model should contain the ManytoManyField

django's .extra(where= clauses are clobbered by table-renaming .filter(foo__in=… subselects

浪子不回头ぞ 提交于 2019-12-03 17:08:05
The short of it is, the table names of all queries that are inside a filter get renamed to u0, u1, ..., so my extra where clauses won't know what table to point to. I would love to not have to hand-make all the queries for every way I might subselect on this data, and my current workaround is to turn my extra'd queries into pk values_lists, but those are really slow and something of an abomination. Here's what this all looks like. You can mostly ignore the details of what goes in the extra of this manager method, except the first sql line which points to products_product.id: def by_status(self

How to add filters to a query dynamically in Django?

旧时模样 提交于 2019-12-03 16:16:46
In my viewSet I am doing a query, queryset= Books.objects.all(); Now from an ajax call I get my filter values from UI i.e. age,gender, etc. of auther.There will be a total of 5 filters. Now the problem which I ran into is how am I going to add filters to my query(only those filters which have any value). What I tried is I checked for individual filter value and did query, but that way it fails as if the user remove the filter value or add multiple filters. Any better suggestion how to accomplish this? Here's a bit more generic one. It will apply filters to your queryset if they are passed as

Django ORM: caching and manipulating ForeignKey objects

我的梦境 提交于 2019-12-03 13:13:43
问题 Consider the following skeleton of a models.py for a space conquest game: class Fleet(models.Model): game = models.ForeignKey(Game, related_name='planet_set') owner = models.ForeignKey(User, related_name='planet_set', null=True, blank=True) home = models.ForeignKey(Planet, related_name='departing_fleet_set') dest = models.ForeignKey(Planet, related_name='arriving_fleet_set') ships = models.IntegerField() class Planet(models.Model): game = models.ForeignKey(Game, related_name='planet_set')

In Django ORM, “values” and “annotate” are not working to group by

删除回忆录丶 提交于 2019-12-03 12:49:16
I have a table like this: Now I want to sum up the meals on each date. I have written the code below. But it doesn't work as I wanted. Model: class Meal(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) date_of_meal = models.DateField() morning_meal = models.SmallIntegerField(default=0) mid_day_meal = models.SmallIntegerField(default=0) night_meal = models.SmallIntegerField(default=0) updated = models.DateTimeField(auto_now=True, auto_now_add=False) timestamp = models.DateTimeField(auto_now=False, auto_now_add=True) class Meta: ordering = ['-updated', '-timestamp']

Changing South Migration Directory

最后都变了- 提交于 2019-12-03 12:31:01
How do you change the location where South looks for an app's migrations? By default, South assumes an app's migrations are in /migrations. However, I've migrated the model of a third-party package which is installed at /usr/local/lib/python-2.6/dist-packages/, so South is looking for migrations there, instead of the location of my custom codebase. In settings.py: SOUTH_MIGRATION_MODULES = { 'books': 'myproject.app_name.migrations', } 来源: https://stackoverflow.com/questions/5994420/changing-south-migration-directory

How can I chain Django's “in” and “iexact” queryset field lookups?

我是研究僧i 提交于 2019-12-03 11:45:01
问题 I have a list of names, e.g.: name_list = ['Alpha', 'bEtA', 'omegA'] Currently I have the following queryset: MyModel.objects.filter(name__in=name_list) I would like to be able to filter the names in a case-insensitive fashion. My first thought was to use the iexact field lookup but it doesn't seem to work with in . How can I use the iexact with the in field lookup for my queryset? Or is there an alternate way to perform this query? 回答1: Here's my solution, which uses Q objects instead: name

Pivoting data and complex annotations in Django ORM

≯℡__Kan透↙ 提交于 2019-12-03 10:50:36
The ORM in Django lets us easily annotate (add fields to) querysets based on related data, hwoever I can't find a way to get multiple annotations for different filtered subsets of related data. This is being asked in relation to django-helpdesk , an open-source Django-powered trouble-ticket tracker. I need to have data pivoted like this for charting and reporting purposes Consider these models: CHOICE_LIST = ( ('open', 'Open'), ('closed', 'Closed'), ) class Queue(models.model): name = models.CharField(max_length=40) class Issue(models.Model): subject = models.CharField(max_length=40) queue =

How can I make a Django model read-only?

我的梦境 提交于 2019-12-03 10:22:45
Is it possible to make a Django model read only? No creating, updating etc. N.B. this question is different to: Make a Django model read-only? (this question allows creation of new records) Whole model as read-only (only concerns the Django admin interface - I'd like the model to be read only throughout the whole app) Override the save and delete methods for the model. How are you planning to add objects to your model? def save(self, *args, **kwargs): return def delete(self, *args, **kwargs): return Database Routers To take more precautions that your model is read-only, you can use the