保存后维护通过信号填充的模型之间的关系

Maintaining Relationships Between Models that are Filled Through Signals After Saving

查看答案以了解我是如何解决的以及我的问题是什么:)

我有一个名为 stars 的基本模型,它包含与其他模型的大部分关系,并且一些其他模型通过信号执行某些脚本,这些脚本将输入数据库有关基本模型的更多信息。我的问题是当 运行 信号时,我不知道如何与已经是基本模型的一对一字段的模型保持关系。如果我试图使用于信号的空模型成为它们实例的外键,它会给我错误,因为它们的实例已经通过它自己的外键连接到星星。我将 post 部分代码放在这里,第一部分是我的模型,然后是我的信号部分:

from django.db import models

# Create your models here.

'''define table with star name and its found relationships in the cards'''

class Star(models.Model):
 name = models.CharField(max_length=100, unique = True, verbose_name = "Star Name")
 
 def __str__(self):
  return self.name


class RA(models.Model):
 name = models.OneToOneField(Star,default = 1,to_field = "name")
 Ra = models.CharField(max_length = 10, help_text=("<h4> <i> e.g. if the RA is 1 hour, 44 minutes, and 38 seconds, input the RA in this manner: <b> 014438 </b> </i> </h4>"), verbose_name = "RA", unique = True)
 def __str__(self):
  return self.Ra


class DEC(models.Model):
 name = models.OneToOneField(Star,default = 1,to_field = "name")
 Dec = models.CharField(max_length = 10, help_text=("<h4> <i> e.g if the DEC is +3 degrees, and 48.6 arc-minutes, input the DEC in this manner: <b> +0348.6 </b> DON'T FORGET THE 0s FOR ONE DIGIT INPUTS. </i> <h4>"), verbose_name = "DEC", unique = True)
 def __str__(self):
  return self.Dec


class RAnew(models.Model):
 ra_new = models.CharField(max_length = 10)
 class Meta:
  verbose_name = "new RA"

 def __str__(self):
  return self.ra_new


class DECnew(models.Model):
 dec_new = models.CharField(max_length = 10)
 class Meta:
  verbose_name = "new Dec"

 def __str__(self):
  return self.dec_new


from .signals import perform_and_save_query, clean_URL, separate_ra, separate_dec
#this connects the astroquery and other signals and runs them yay

信号脚本:

#create signals to run scripts

from astroquery.simbad import Simbad
from .models import Aliases, ReferenceURL, Bibcode, RA, DEC, RAnew, DECnew, Star
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.core.exceptions import ValidationError

@receiver(post_save, sender=RA)

def separate_ra(sender, **kwargs):
    if kwargs.get('created', False):
        RA.objects.get_or_create(Ra=kwargs.get('instance'))
        z = kwargs['instance']
        z = str(z)
        if len(z) < 6:
            raise ValidationError({'Ra': ["The RA needs to be 6 characters long, make sure you're using 0s in one digit pairs of numbers.",]})
        else:
            makenew = z[:2]+' h '+z[2:4]+' m '+z[4:]+' s'
            d = RAnew(ra_new = makenew)
            d.save()

@receiver(post_save, sender = DEC)

def separate_dec(sender, **kwargs):
    if kwargs.get('created', False):
        DEC.objects.get_or_create(Dec=kwargs.get('instance'))
        j = kwargs['instance']
        j = str(j)
        if len(j) < 7:
            raise ValidationError({'Dec': ["The Dec needs to be greater than 7 characters long, use zeros for 1 digit numbers and .0 in case of no decimals at the end",]})
        else:
            makenewdec = j[:3]+' degrees '+j[3:]+' arcmin'
            e =DECnew(dec_new = makenewdec)
            e.save()

唯一保持与其实例关系的信号是第一个 "perform and save query",因为它直接与模型打交道 "stars"。有没有办法让新保存的 "bibcode"、"New RA" 和 "New Dec" 可以保持与他们的实例或他们的星的关系?

我忘记了为其他信号保存对象的格式“model.objects.get(field = x)”,这就是我收到错误的原因。我以为这与模型是星星的外键有关,但这只是语法错误!

除此之外,我最近了解了模型的正则表达式验证器,这消除了我需要一个信号来验证条目编写方式的需要,因为正则表达式验证器可以真正正面处理这种需求。我强烈推荐它!