Reputation: 244
I have issues with the two following models. The first model (QueryJob) is referenced by the second model(PropertyQuery) by a many to many field( queryID = models.ForeignKey(QueryJob,blank=True,null=True)) which necessitates that QueryJob
precedes PropertyQuery
.
How PropertyQuery objects are created by calling QueryJob.createqueries() which requires that PropertyQuery preceeds QueryJob.
Code is below. Is there a better way to approach this problem?
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)
Second model
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"
Upvotes: 0
Views: 67
Reputation: 174622
Switch the order of these statements, so that your QueryJob
is created first, then your PropertyQuery
models are created:
def save(self, *args, **kwargs):
super(QueryJob, self).save(*args, **kwargs)
self.createqueries()
In your createqueries()
method, you can refer to self
when you need to create a link. Do not create a link to the primary key directly as this won't work - you don't realize its not working because you have a blank except clause that is catching the exceptions raised:
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()
Upvotes: 1