Tuesday, July 31, 2012

Re: merge data from multiple models


> Hmm, I guess I'm missing the "real world use case" for this. Most 
> importantly, I'm missing how this slow model relates to the fast model 
> and what kind of query it is executing. I'm especially curious about the 
> "as this statement is executed as an instance of the model" bit. 

Ok, I'll try to explain this better. The fast model is coupled directly to a table. The Slow model is not, it is activated by a specific sql query that depends on a specific condition.
AFAIK to get data from the slow model, I do:
SLOMO = SlowModel.objects.raw( SQLstatement ,[CONDITION]) 

However, if I set the primary_key of SlowModel to OneToOne (pointing to FastModel), as suggested above, and try to retreive SlowModel data from FastModel  (FastModelInstance.slow) I get an error that data is missing. My guess was that this was because I did not manage to tell the instance of Fastmodel that it first needs to pass the SQL to its SlowModel connection before I can ask it questions.

#-------------------------------
class FastModel(models.Model):
    idgroup = models.IntegerField(primary_key=True)
    field1 = models.IntegerField()
    field2 = models.CharField(max_length=250, blank=True)
    field3 = models.CharField(max_length=250, blank=True)
    class Meta:
        db_table = u'my_db_table'
   
class SlowModel(models.Model): 
    rid_fastmodel = models.IntegerField(primary_key=True)
    calcfield1 = models.TextField(blank=True)
    calcfield2= models.TextField(blank=True)

SQLstatement = "SELECT rid_fastmodel, calcfield1, calcfield2 from (SELECT ....) JOIN (...) JOIN (...) WHERE some_condition = %s"

Is this better?

thanks
joris

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/TPkzlZJ7NfkJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment