class Categories(models.Model):
id = models.ForeignKey('auth.User',primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
def __str__(self):
return Categories.name
class Meta:
order_with_respect_to = 'id'
class Specializations(models.Model):
id = models.ForeignKey('auth.User',primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
categories = models.ForeignKey(Categories, on_delete=models.CASCADE)
def __str__(self):
return Specializations.name
courses.Categories.id :( fields.W342)在ForeignKey上设置unique = True与使用OneTo具有相同的效果 OneField。
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
courses.Courses.id :( fields.W342)在ForeignKey上设置unique = True与使用OneToOne具有相同的效果 字段。
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
courses.Specializations.id :( fields.W342)在ForeignKey上设置unique = True与使用a具有相同的效果 OneToOneField。
虽然关系是一对多,但这个警告或错误会引发!不是一对一的
答案 0 :(得分:3)
您设计模型的方式基本上是OneToOne
关系,因为您将ForeignKey
设置为primary_key
(这会自动适用unique=True
)。
您真的希望用户成为primary_key
或Categories
的{{1}}吗?
我想你想要这样的东西:
Specializations
有了这个,class Categories(models.Model):
user = models.ForeignKey('auth.User')
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
def __str__(self):
return self.name
class Specializations(models.Model):
user = models.ForeignKey('auth.User')
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
categories = models.ForeignKey(Categories, on_delete=models.CASCADE)
def __str__(self):
return self.name
可以有很多User
和Categories
我还将Specializations
方法更改为__str__