我有一个看起来像这样的模型,并且可以正常运行:
axios.get(`api.openweathermap.org/data/2.5/forecast?id=2193733&appid=MYAPIKEY`)
.then(response => {
console.log(response);
})
每个用户只能有一个def BookModel(TimeStampedModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
bookname = models.CharField()
class Meta:
constraints = [
models.UniqueConstraint(fields=['user ', 'bookname'],
name='StackOverflow.Question')
]
和一个特定的BookModel
。
但是,现在我要介绍bookname
LibraryModel
行为应保持不变:仍应根据关联的用户强制执行约束,但是用户ID现在位于LibraryModel中。
我运行迁移时,它显示为class LibraryModel(TimeStampedModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=32)
def BookModel(TimeStampedModel):
library = models.ForeignKey(LibraryModel, on_delete=models.CASCADE)
bookname = models.CharField()
class Meta:
constraints = [
models.UniqueConstraint(fields=['library_user', 'bookname'],
name='StackOverflow.Question')
]
。
我也尝试了django.core.exceptions.FieldDoesNotExist: BookModelhas no field named 'library_user'
和fields=['library__user'
,但无济于事。
这有可能吗?还是应该在两个模型中都使用fields=['library.user'
属性,违反某些DRY原理?