查看答案,看看我是如何解决它的,以及我的问题是什么:)
我有一个名为stars的基本模型,它包含与其他模型的大多数关系,其他一些模型通过一个信号来执行某些脚本,这些脚本将向数据库中输入有关基本模型的更多信息。我的问题是,当运行信号时,我不知道如何维护与已经是基本模型的一对一字段的模型的关系。如果我试图将用于信号的空模型设置为它们实例的外键,它会给我错误,因为它们的实例已经通过它自己的外键连接到了star。我将在这里发布我的部分代码,首先是模型的一部分,然后是信号的一部分:
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()
维护与其实例关系的唯一信号是第一个“执行并保存查询”,因为它直接处理模型"stars“。有没有办法让新保存的"bibcode“、"New RA”和"New Dec“能够与它们的实例或star保持关系?
发布于 2017-07-07 03:38:57
我忘记了格式“model.objects.get(field = x)”来为其他信号保存一个对象,这就是为什么我得到一个错误的原因。我认为这与模型是星号的外键有关,但这只是一个语法错误!
为了补充这一点,我最近了解到模型的正则表达式验证器,这消除了我对验证条目编写方式的信号的需要,因为正则表达式验证器可以真正正面处理这种类型的需求。我强烈推荐它!
https://stackoverflow.com/questions/44957074
复制相似问题