看看答案,看看我是如何解决它的,以及我的问题是什么:)
我有一个名为星的基本模型,其中包含与其他模型的大多数关系,而其他一些模型则通过信号来执行某些脚本,这些脚本将在数据库中输入有关基本模型的更多信息。我的问题是在运行信号时,我不知道如何维护与已经是基本模型的一对一字段的模型的关系。如果我尝试将用于信号的空模型作为其实例的外键,它会给我错误,因为它们的实例已经通过自己的外键连接到星号。我会在这里发布我的部分代码,我的模型的第一部分,然后是我信号的一部分:
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
&#13;
信号脚本:
#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()
&#13;
维持与其实例的关系的唯一信号是第一个&#34;执行并保存查询&#34;因为它直接涉及模型&#34;明星&#34;。有没有办法让新保存的&#34; bibcode&#34;,&#34;新RA&#34;和&#34;新的12月&#34;可以保持他们与他们的实例或他们的明星的关系吗?
答案 0 :(得分:1)
我忘记了格式&#34; model.objects.get(field = x)&#34;为其他信号保存一个对象,这就是我收到错误的原因。我认为这与模型是星星的外键这一事实有关,但这只是语法错误!
除此之外,我最近还了解了模型的正则表达式验证器,这消除了我需要一个信号来验证条目的写入方式,因为正则表达式验证器可以真正处理这种类型的需求。我强烈推荐它!