我与django用户模型建立了外键关系,正向查找工作正常,但是当我尝试向后移动时抛出此错误:
'QuerySet'对象没有属性'urlpost_set'
我也试过相关的名字!另请注意,Catagory到PostUrl和PostUrl到Catagory的工作正常!
我的models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Catagory(models.Model):
title = models.CharField(max_length=15, unique=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'catagory'
class UrlPost(models.Model):
STATUS_CHOICES = (
('public', 'Public'),
('private', 'Private'),
)
profile = models.ForeignKey(User, related_name='user_post', on_delete=models.CASCADE)
catagory = models.ForeignKey(Catagory, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
url = models.URLField()
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='public')
note = models.TextField(blank=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
class Meta:
ordering = ['-created']
verbose_name_plural = 'url Post'
def __str__(self):
return self.title
答案 0 :(得分:2)
您在定义related_name='user_post'
模型与ForeignKey
之间的User
关系时设置了UrlPost
。
您必须在查询集中使用.user_post.all()
而不是.urlpost_set.all()
。