在django模型中创建ForeignKey时,django还会在数据库中创建_id列。我试图弄清楚如何以django形式显示这个值,但似乎无法让它工作。有了我在下面的category_id字段出现在窗体中,但值本身不?
市场/ models.py
class Category(models.Model):
category = models.CharField(null=True)
category_code = models.IntegerField(unique=True)
商品/ models.py
class Product(models.Model):
created = models.DateTimeField(default=now)
name = models.CharField(max_length=200, unique=True)
category = models.ForeignKey('marketplace.Category', blank=True, null=True, to_field='category_code')
商品/ forms.py
class Form(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(Form, self).__init__(*args, **kwargs)
self.fields['category_id'] = self.instance.category_id
class Meta:
model = Product
fields = ['name', 'created']