在Django模型中添加一对多关系

时间:2019-04-15 10:43:30

标签: python django model one-to-many

我的Django-REST应用程序中有两个模型。

一个ProjectRequest和一个ContactRequest

我想这样做,每个Projectrequest contains a list of the refered Contactrequests.

class ProjectRequest(models.Model):
project_name = models.CharField(max_length=50)
company_name = models.CharField(max_length=50)
#make array of technologiestechnologies = models.ArrayField(base_field=) (blank=True)
project_description = models.CharField(max_length=200)
project_type = models.CharField(max_length=30)
budget_estimation = models.IntegerField(
    default=1000,
    validators=[
    MinValueValidator(1800),
    MaxValueValidator(5000000)
])
#time_estimation = models.DateTimeField(default=None, blank=True, null=True)


class ContactRequest(models.Model):
topic = models.CharField(max_length=30)
description = models.CharField(max_length=200)
time = models.CharField(max_length=15)
project_request = models.ForeignKey(ProjectRequest, 
on_delete=models.CASCADE)

到目前为止,我已经建立了一个具有外键的关系,该关系到现在为止仍然有效。但是,我想扩展功能,以便ProjectRequest包含所有projectrequest的列表。我尝试了几个不同的字段,没有任何运气,而文档中我只能找到ManyToManyOneToOne的字段。如何实现?

2 个答案:

答案 0 :(得分:1)

有很多方法可以实现您想要的。为此,让我们在名为contact_requests的模型中添加一个reverse relation

project_request = models.ForeignKey(ProjectRequest, on_delete=models.CASCADE, related_name="contact_requests")

现在,您可以使用PrimaryKeyRelatedField来显示附加到每个ContactRequest上的ProjectRequest的主键。

class ProjectRequestSerializer(serializers.ModelSerializer):
    contact_requests = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

    class Meta:
        model = ProjectRequest
        fields = ('contact_requests', 'company_name', ...)  # other fields 

或者,如果需要每个contact_requests的所有值,则可以像这样使用nested relationship

class ProjectRequestSerializer(serializers.ModelSerializer):
    contact_requests = ContactRequestSerializer(many=True, read_only=True)

    class Meta:
        model = ProjectRequest
        fields = ('contact_requests', 'company_name', ...) # and so on

答案 1 :(得分:1)

您可以在ProjectRequest类中添加一个属性函数,以重新运行与该ProjectRequest相关的所有ContactRequest,就像这样...

class ProjectRequest(models.Model):
    project_name = models.CharField(max_length=50)
    company_name = models.CharField(max_length=50)
    #make array of technologiestechnologies = models.ArrayField(base_field=) (blank=True)
    project_description = models.CharField(max_length=200)
    project_type = models.CharField(max_length=30)
    budget_estimation = models.IntegerField(
        default=1000,
        validators=[
        MinValueValidator(1800),
        MaxValueValidator(5000000)
    ])
    #time_estimation = models.DateTimeField(default=None, blank=True, null=True)

    @property
    def contact_requests(self):
        return ContactRequest.objects.filter(project_request=self)


class ContactRequest(models.Model):
    topic = models.CharField(max_length=30)
    description = models.CharField(max_length=200)
    time = models.CharField(max_length=15)
    project_request = models.ForeignKey(ProjectRequest, 
    on_delete=models.CASCADE)