108👍
I haven’t tried this, but it seems that since Django 1.0 you can do something like:
class Foo(models.Model):
foo = models.ForeignKey(Bar, to_field='bar')
Documentation for this is here.
18👍
Yes and no. The FK relationship is described at the class level, and mirrors the FK association in the database, so you can’t add extra information directly in the FK parameter.
Instead, I’d recommend having a string that holds the field name on the other table:
class ValidationRule(models.Model):
other = models.ForeignKey(OtherModel)
other_field = models.CharField(max_length=256)
This way, you can obtain the field with:
v = ValidationRule.objects.get(id=1)
field = getattr(v, v.other_field)
Note that if you’re using Many-to-Many fields (rather than a One-to-Many), there’s built-in support for creating custom intermediary tables to hold meta data with the through option.
- [Django]-Non-global middleware in Django
- [Django]-Update only specific fields in a models.Model
- [Django]-Django "get() got an unexpected keyword argument 'pk'" error
2👍
You need to use "to_field" in "models.ForeignKey()" to set other field in other model:
The field on the related object that the relation
is to. By default, Django uses the primary key of the related object.
If you reference a different field, that field must have unique=True.
For example, as a foreign key, "categories" field in "Product" model references "name" field in "Category" model as shown below. *Be careful, the referenced field "name" in "Category" model needs "unique=True" or "primary_key=True" otherwise there is an error:
# "models.py"
from django.db import models
# "unique=True" or
class Category(models.Model): # "primary_key=True" are needed
name = models.CharField(max_length=100, unique=True)
class Product(models.Model):
name = models.CharField(max_length=100)
categories = models.ForeignKey(
Category,
to_field='name', # ← Here
on_delete=models.PROTECT
)
- [Django]-Negating a boolean in Django template
- [Django]-Access Django model's fields using a string instead of dot syntax?
- [Django]-WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8)