Django模型不允许一对多关系

时间:2018-10-14 01:17:00

标签: django django-models

在为Django创建此模型集方面,我非常费劲。目前,我正在创建一个网站,用户可以在该网站上单击该文章,以决定他或她喜欢该文章。我设置了模型以包括文章,收藏的文章和用户。但是,目前,我设置它的方式是只允许用户拥有一个喜欢的文章,而不是两个。我正在使用ForeignKey字段,但不起作用。他们在这里

from django.db import models
# users/models.py
from django.contrib.auth.models import AbstractUser
from django.db.models.signals import post_save
from django.dispatch import receiver
import uuid

class Article(models.Model):
    title = models.CharField(max_length=120, primary_key=True)
    content = models.TextField()
    url=models.CharField(max_length=200, null=True)
    category = models.CharField(max_length=250, null =True)


    def __str__(self):
        return self.title

class CustomUser(AbstractUser):
    username = models.CharField(max_length=50, unique=True, primary_key=True)
    git = models.CharField(max_length=200, null=True)
    homepage = models.CharField(max_length=250, null=True)

    def __str__(self):
        return str(self.username)

class FavoriteArticles(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='user', null=True)
    fav_title = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='fav_title', null=True)
    reasons = models.CharField(max_length=120, null=True)
    favcategory = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='favcategory', max_length=250, null =True)


    # def __str__(self):
    #     return self.user

0 个答案:

没有答案