具有多对多关系的 Django 模型在 models.py 中导致冲突的排序要求

Django models with a many to many relationship causing conflicting ordering requirements in models.py

我对以下两个模型有疑问。第一个模型 (QueryJob) 被第二个模型 (PropertyQuery) 通过多对多字段引用 (queryID = models.ForeignKey(QueryJob,blank=True,null=True)),这需要 QueryJob 在前面PropertyQuery.

如何通过调用 QueryJob.createqueries() 创建 PropertyQuery 对象,这要求 PropertyQuery 在 QueryJob 之前。

代码如下。有没有更好的方法来解决这个问题?

class QueryJob(models.Model):
  queryUID = models.AutoField(primary_key=True)
  client = models.ManyToManyField(Client,blank=True)
  Json = JSONField()
  street_column =models.TextField()
  state_column =models.TextField(blank=True) 
  suburb_column =models.TextField(blank=True) 
  postcode_column =models.TextField(blank=True) 
  def createqueries(self):
    #json_data = json.loads(self.Json)
    print self.Json
    for each in self.Json:
      try:
        Street = each[self.street_column]
        State = each[self.state_column]
        Suburb = each[self.suburb_column]
        Postcode = each[self.postcode_column]
        q = PropertyQuery(street_address = Street, state=State ,suburb = Suburb,postcode=Postcode,queryID=self.queryUID )
        q.save()
      except:
        pass



  def save(self, *args, **kwargs):
    self.createqueries()
    super(QueryJob, self).save(*args, **kwargs)

第二个模型

class PropertyQuery(models.Model):
  queryID = models.ForeignKey(QueryJob,blank=True,null=True)
  linkedproperty = models.ForeignKey(Property,blank=True,null=True)
  street_address = models.CharField(max_length=255, db_index=True,null=True)
  suburb = models.CharField(max_length=120, db_index=True,blank=True,null=True)
  state = models.CharField(max_length=3, db_index=True,blank=True,null=True)
  postcode = models.IntegerField(max_length=4, db_index=True, blank=True,null=True)
  matcheduncertainty = models.PositiveSmallIntegerField(blank=True,null=True)
  def search_for_a_match(self):
    if self.postcode:
      print self.postcode

      print Property.objects.filter(postcode=self.postcode)
      try:
        property_list = Property.objects.filter(postcode=self.postcode)

        print property_list 
      except:
        print "no properties in that postcode"
        return
    elif self.suburb:
      try:
        property_list = Property.objects.filter(suburb=self.suburb)
        print property_list 
      except:
        print "no properties in that suburb"
    elif self.state:
      try:
        property_list = Property.objects.filter(state=self.state)
        print property_list 
      except:
        print "no properties in that state"
        return
    else:
      print "no properties found"
      return
    for possible in property_list:
      if possible.street_address == self.street_address:
        self.linkedproperty = possible
        self.matcheduncertainty = 100
        return
    else:
      print "we will need to try something else"

调换这些语句的顺序,以便先创建您的 QueryJob,然后再创建您的 PropertyQuery 模型:

def save(self, *args, **kwargs):
    super(QueryJob, self).save(*args, **kwargs)
    self.createqueries()

在你的createqueries()方法中,需要创建link的时候可以参考self。不要直接为主键创建 link,因为这不会起作用——您没有意识到它不起作用,因为您有一个空白的 except 子句正在捕获引发的异常:

def createqueries(self):
    #json_data = json.loads(self.Json)
    print self.Json
    for each in self.Json:
        Street = each.get(self.street_column)
        State = each.get(self.state_column)
        Suburb = each.get(self.suburb_column)
        Postcode = each.get(self.postcode_column)
        q = PropertyQuery(street_address = Street,
                          state=State,
                          suburb = Suburb,
                          postcode=Postcode,
                          queryID=self)
        q.save()