Suppose I have a field f in my model defined as follows as a foreign key:
f = models.ForeignKey(AnotherModel)
When Django syncs database, the filed created in db will be called f_id, automatically suffixed with '_id'.
The problem is I want this field in db named exactly as what I defined in model, f in this case. How can I achieve that?
Well it turns out there's a keyword argument called db_column. If you want the field named 'f' in the database table, it's just as simple as:
f = models.ForeignKey(AnotherModel, db_column='f')
Further reference:
The name of the database column to use for
this field. If this isn't given, Django will use the field's name.
If your database column name is an SQL reserved word, or contains
characters that aren't allowed in Python variable names -- notably,
the hyphen -- that's OK. Django quotes column and table names behind
the scenes.
https://docs.djangoproject.com/en/2.2/ref/models/fields/#db-column
Related
I have following two query set as properties of django model
1 - Comment.objects.filter(ForeignKeyField=self)
2 - Comment.objects.filter(ForeginKeyField=self.id)
I am not why both these lines are giving same result , namely CommentObjectList ? Why I am able to filter the Comment Resultset with self and self.id both ??
The FK field actually holds the primary key for the related record. So
Comment.objects.filter(ForeginKeyField=self.id)
should give you the result you want (assuming self here is an instance of the foreign key model)
Comment.objects.filter(ForeginKeyField=self)
would try and stuff the entire instance in and match that, so you'd be matching an int or UUID (depending on what kind of key you have) with an instance of the FK object, and of course this will not match.
Side note: field names should use underscores in Django, by convention, so foreign_key_field, not ForeignKeyField.
I have to table with relation.
State
id
name
City
id
name
state
Which is better in performance?
city.state.id or city.state_id
city.state_id is better anyway. city.state will do another fetch from database.You can avoid this using select_related.If you need only id of foriegn key, no need of select_related here.Just do city.state_id(since foriegn key id will fetch in the query which gives city object).
city.state_id is better than city.state.id. Because It makes only a query instead of two.
BTW, You can use Django Debug Toolbar for debugging queries.
the <field>_id field you see is the database column name
docs
Behind the scenes, Django appends "_id" to the field name to create its database column name. In the above example, the database table for the Car model will have a manufacturer_id column
So this means it doesn't need to make a separate query to retrieve the foreign key instance (See Select a single field from a foreign key for more details).
But this assumes you haven't used select_related or prefetch_related
Is there a way if a database column uses a number (0,1,2,3,4,5) to set it up as Django model? The database is already created with mongoDB, so I can't make any changes to it, so any easy/fast workaround?
You can set up a Django model with different field names and set db_column param to each field so that it corresponds to the database column name. For example:
class Example(models.Model):
firstField = models.IntegerField(db_column='0')
secondField = models.CharField(db_column='1')
...
No you can not assign to literals in Python. You could prefix the column names while creating the fields, e.g. _0, but generally a number isn't considered a good field name
in models i have start and end date.
How to get all element where start and end date are diffrents.
>>> Entry.objects.exclude(start = end)
>>> NameError: name 'end' is not defined
I have no idea please help.
https://docs.djangoproject.com/en/dev/topics/db/queries/#filters-can-reference-fields-on-the-model
In the examples given so far, we have constructed filters that compare the value of a model field with a constant. But what if you want to compare the value of a model field with another field on the same model?
Django provides the F() object to allow such comparisons. Instances of F() act as a reference to a model field within a query. These references can then be used in query filters to compare the values of two different fields on the same model instance.
For your case, the following should work.
from django.db.models import F
Entry.objects.exclude(start=F('end'))
I have a model that contains a FileField. I want to search for a specific filename. How do I do it? I was trying:
MyModel.objects.get(document__name=FOO)
I got a Join on field 'document' is not permitted.
Thanks!
The attributes of a FileField are not stored in the database, and cannot be used in a query. For example, the name is simply the upload_to string plus the filename. If you want to store extra data about the file you have to put that data into other fields on the database, as the example documentation shows with a Car having a name of "57 Chevy".
Also, typically the double underscore in Django's ORM denotes following a database relationship, either a ForeignKey or a ManyToMany. So in the example ORM call you provided, I would assume that MyModel had a field document that was either a ForeignKey or ManyToMany to another model, and that other model has a field called name. Which doesn't sound like is the case.
Hope that helps some.
Do this instead:
MyModel.objects.get(document__icontains='FOO')
You can filter on document, and it'll filter by the string that is the path on disk to the file.