On Feb 5, 3:50 pm, rc <[email protected]> wrote:
> I am newbie to Django and I am struggling to get my arms around DJango
> and it's data access api (models).
>
> I have these models:
>
> class Profile(models.Model):
> profile_id = models.AutoField(primary_key=True)
> profile_name = models.CharField(max_length=75)
> def __unicode__(self):
> return self.profile_id
> def __unicode__(self):
> return self.profile_name
> class Meta:
> db_table = 'profile'
>
> class Testcase(models.Model):
> test_id = models.AutoField(primary_key=True)
> test_name = models.CharField(max_length=300)
> src = models.ForeignKey(Source, null=True, blank=True)
> bitrate = models.IntegerField(null=True, blank=True)
> test_type = models.CharField(max_length=300)
> output_resolution = models.CharField(max_length=15, blank=True)
> def __unicode__(self):
> return self.test_name
> class Meta:
> db_table = 'testcase'
>
> class Results(models.Model):
> result_id = models.AutoField(primary_key=True)
> date = models.DateTimeField()
> test = models.ForeignKey(Testcase)
> profile = models.ForeignKey(Profile)
> status = models.CharField(max_length=30)
> graph = models.BlobField(null=True, blank=True)
> y_psnr = models.DecimalField(null=True, max_digits=5,
> decimal_places=2, blank=True)
> u_psnr = models.DecimalField(null=True, max_digits=5,
> decimal_places=2, blank=True)
> v_psnr = models.DecimalField(null=True, max_digits=5,
> decimal_places=2, blank=True)
> yuv_psnr = models.DecimalField(null=True, max_digits=5,
> decimal_places=2, blank=True)
> def __unicode__(self):
> return self.result_id
> class Meta:
> db_table = 'results'
>
> and I want to be able to display this data:
>
> select result_id, date, profile_name, test_name, status, y_psnr,
> u_psnr, v_psnr, yuv_psnr
> from profile, testcase, results
> where profile.profile_id = results.profile_id
> and testcase.test_id = results.test_id
>
> Which is very easy to do with raw sql, but struggling to do it the
> "django' way.
>
> Any ideas?
> I have also tried to use raw sql, but struggled to get it to work in
> my views and templates.
The Django ORM is there to help you. If you don't find it easy, don't
use it. However, the simple way of doing it would be something like
this:
for testcase in Testcase.objects.all():
print testcase.status
for result in testcase.result_set.all():
print result.result_id, result.date,
result.profile.profile_name, \
result.status, result.y_psnr, \
result.u_psnr, result.v_psnr, result.yuv_psnr
You can make that much more efficient via proper use of
select_related, but that's the general idea.
Not related to your problem, but you've got an issue with your inner
Meta classes - each time they are indented under the __unicode__
method. They should be one indent level back.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---