如何将两个应用程序的主键链接在一起

时间:2018-01-02 20:59:41

标签: python django windows web

基本上我有两个共享相同名称字段的应用程序,我想知道如何基于检查其中一个应用程序中的名称模型是否与其他应用程序的名称模型匹配来生成主键,并分配第一个主键应用到第二个 我尝试使用get_pk功能,如下所示,但它没有用 感谢

模型

class InfoPedia(models.Model):
    #User model due to association
    user       =models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
    Name        =models.ForeignKey(MainPage,on_delete=models.CASCADE)
    #Information unique to this app
    Location      =models.CharField(max_length= 50,null=True,blank=True)
    Information        =models.TextField(null=True,blank=True)
    TrackListing=models.TextField(null=True,blank=True, help_text="Seperate by Coma")
    Published=models.BooleanField(default=True)
    Timestamp=models.DateTimeField(auto_now=True)
    Updated=models.DateTimeField(auto_now=True)



    def _get_pk(self, meta=None):
        Tempn=MainPage.objects.get(Name=self.Name)
        if Tempn.Name==self.Name:
            self.pk=Tempn.pk
        return self.pk
#     

#     def get_absolute_url(self):
#         return reverse("InfoPedia:details", kwargs={"pk":self.pk})
#     def __str__(self):
#         return self.Name

        # You use this to make each item a hyperlink based on the detail view and put it the detailview template
    def get_absolute_url(self):
#         return f"/Blog/{self.slug}"
        return reverse('InfoPedia:DetailView', kwargs={"pk":self.pk})

    class Meta:
        ordering=["-Updated","-Timestamp"] #orranges in order of updated

    def get_tracklist(self):
        return self.TrackListing.split(",")

def Information_create_pre_save( instance, sender, **kwargs):
        instance.Information=retriever(instance.Name)
#         instance.TrackListing=WikiPage.categories

0 个答案:

没有答案