I've been looking into django joins, and I can see how I can select related
fields with .select_related but I can't seem to get it to work how I want.
I basically have summary tables that are linked to the customers table with
customer_id FK's, I want to select the customer name from the customers
field for every value returned for each of the summaries.
I included pseudo code to show what im trying to do.
sql would be = ''' select a.customer_name, b.vm_name, b.vm_cpu,
b.vm_mem from customers a, vms b where a.customer_id = b.customer_id'''
how do I do that with django. That way when I loop through the returned
values
for value in sql:
list.append(value.1, value.2, value.3)
that way I can associate and output the customer_name with each field.
Heres what I'm trying to do:
compute_usages = ComputeUsages.objects.filter(customer_id = customer_id
).filter(load_date = datetime(year, day, selected_month)).select_related()
for row in compute_usages:
if not 'Microsoft' in row.guest_os:
compute_return_query_linux.append((row.customers.
customer_name, row.vm_name, row.vm_id, row.core_hours, row.ram_hours, row.
guest_os, row.provisioned_cores, row.provisioned_ram, row.datetime, row.
load_date))
else:
compute_return_query_microsoft.append((row.customers.
customer_name, row.vm_name, row.vm_id, row.core_hours, row.ram_hours, row.
guest_os, row.provisioned_cores, row.provisioned_ram, row.datetime, row.
load_date))
obviously that doesn't work.
basically that one is for specific customers, but there is a case where
company employees with admin rights can query the entire database for all
customers and create a usage report so I need to associate customer_names
with the query.
Any ideas on how to do this with django?
Models:
class Customers(models.Model):
customer_id = models.BigIntegerField(primary_key=True, editable=
False)
customer_name = models.CharField(max_length=100)
inactive = models.CharField(max_length=1)
datetime = models.DateTimeField()
class Meta:
managed = True
db_table = 'customers'
def __unicode__(self): # Python 3: def __str__(self):
return self.customer_name
def clean(self):
if self.inactive != 'Y' and self.inactive != 'N':
raise ValidationError('Please enter a Y or N')
class ComputeUsages(models.Model):
compute_usage_id = models.AutoField(primary_key=True, editable=False
)
customer = models.ForeignKey(Customers)
vm_name = models.CharField(max_length=4000)
vm_id = models.BigIntegerField()
core_hours = models.DecimalField(max_digits=15, decimal_places=2)
ram_hours = models.DecimalField(max_digits=15, decimal_places=2)
guest_os = models.CharField(max_length=100)
provisioned_cores = models.BigIntegerField()
provisioned_ram = models.BigIntegerField()
load_date = models.DateField()
datetime = models.DateTimeField()
class Meta:
managed = True
db_table = 'compute_usages'
def __unicode__(self): # Python 3: def __str__(self):
return self.vm_name
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/306a1cae-5901-4468-8834-a8d578a2929c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.