当前位置: 动力学知识库 > 问答 > 编程问答 >

save - Django - creating foreignkey for "to_field" if does not exist

问题描述:

I've got 2 models (+2 not important).

Link custom save method is supossed to create kod automatically on create, and on update keeping kod unchanged.

On create Lead, I want to create Link automatically if it does not exist. Whats the best method to do that?

It's my second day of searching for best approaches when it comes to custom save methods with foreignkeys but im lack of ideas.

class Link(models.Model):

landing = models.ForeignKey(Landing, default=1, on_delete=models.PROTECT)

partner = models.ForeignKey(Partner, default=1, on_delete=models.PROTECT)

kod = models.CharField(max_length=16, unique=True, blank=True, null=False)

def save(self, *args, **kwargs):

if self.kod is None:

self.kod = id_generator(16)

if len(self.kod) != 16:

self.kod = id_generator(16)

if self.pk is not None:

current = Link.objects.get(pk=self.pk)

if current.kod != self.kod:

self.kod = current.kod

super(Link, self).save(*args, **kwargs)

class Lead(models.Model):

link = models.ForeignKey(Link, to_field='kod', on_delete=models.PROTECT)

transactionid = models.CharField(max_length=64, blank=True)

**Edit:**Using:

link = models.ForeignKey(Link, to_field='kod', default=Link.objects.get_or_create(kod='autogenerated123')[0], on_delete=models.PROTECT)

Is working well, but only if there's a Partner and Landing page:

class Link(models.Model):

landing = models.ForeignKey(Landing, default=1, on_delete=models.PROTECT)

partner = models.ForeignKey(Partner, default=1, on_delete=models.PROTECT)

django.db.utils.ProgrammingError: relation "appname_models_link" does not exist

LINE 1: ...ls_link"."opis", "appname_models_link"."created" FROM "appname_model...

网友答案:

Add this method below your Lead class:

class Lead(models.Model):
    link = models.ForeignKey(Link, to_field='kod', on_delete=models.PROTECT)
    transactionid = models.CharField(max_length=64, blank=True)

@receiver (pre_save, sender=Lead)
def pre_save_handler(sender, instance, **kwargs):
    if instance.link is None:
        // Create your link here and then...
        instance.link = your_link_here
        instance.save()

I hope I have been helpful

网友答案:

Using custom save methods really is the best way to achieve this. You could also use signals but this tends to be harder to maintain.

One way to make this "cleaner" could be to create mixins your model class can inherit from. You could also define kwargs which turn of that behaviour if you worry for that. Depending on your requirements your rest api could be a place to handle that but most of the times i would go with something like you already did.

You mean this?

class Lead(models.Model):
    link = models.ForeignKey(Link, to_field='kod', on_delete=models.PROTECT)
    transactionid = models.CharField(max_length=64, blank=True)

    def save(self, *args, **kwargs):
      if self.pk is None and hasattr(self,'link') is False:
        self.link = Link()
        self.link.save()
      super(Lead, self).save(*args, **kwargs)
分享给朋友:
您可能感兴趣的文章:
随机阅读: