Re: iterating over a queryset?
Daniel Roseman adviced me to use the Shell. This was a fantastic idea, and one I should have thought of myself! Anyway this helped me realize that I was referencing related models the wrong way. For some reason I thought that you should reference the model name and not the foreign key field name. It might just be me, but the documentation could seem a bit ambiguous on this point as all field names are lower case model names.
Thanks for the help!
Den tirsdag den 17. september 2013 22.29.38 UTC+2 skrev Johan Bergkvist:
-- Thanks for the help!
Den tirsdag den 17. september 2013 22.29.38 UTC+2 skrev Johan Bergkvist:
Hi
I'm quite new to both django and python, and OOP for that matter. I do have some embedded programming experience though.
I have a question regarding querysets and dictionaries, I think.
I was trying to build a model that provides an overview for subscribed customers over their services and subscription status, etc.
I have a model like this:
class Customer (models.Model):
user_id = models.AutoField( primary_key=True )
auth_user = models.OneToOneField( User)
subscription_type = models.ForeignKey( SubscType, related_name='type', null = 'true', blank = 'true' )
first_name = models.CharField( max_length = 64, verbose_name = 'Fornavn' )
last_name = models.CharField( max_length = 64, verbose_name = 'Efternavn' )
email_address = models.EmailField( max_length = 75, verbose_name = 'eMail adresse' )
join_date = models.DateField(auto_now_add = 'True', verbose_name = 'Oprettet dato')
delivery_address = models.ForeignKey( Address, related_name='delivery', null = 'true', blank = 'true', verbose_name = 'Leveringsadresse')
address = models.ForeignKey( Address, related_name='home', verbose_name = 'Hjemmeadresse' )
phone_number = models.IntegerField(max_length = 10, verbose_name = 'Telefon nummer')
mobile_number = models.IntegerField(max_length = 10, blank = 'true', null = 'true', verbose_name = 'Mobilnummer')
image = models.ImageField(upload_to = 'Customer_img', blank = 'true', verbose_name = 'Billede')
class customerHistory (models.Model):
customer_id = models.ForeignKey( Customer, related_name ='customer' )
used_service = models.ManyToManyField(Services, related_name='services' )
action_date = models.DateField(auto_now_add = 'True')
notes = models.TextField(verbose_name = 'Noter til handling')
views like so:
class CustomerInfo(TemplateView):
#info page, displays base info about customer such as address, phone number and subscription entries
template_name = "subscribtions/info.html"
def get_context_data(self, **kwargs):
context = super(CustomerInfo, self).get_context_data(**kwargs)
context = Customer.objects.filter( auth_user = self.request.user ).values(
'first_name',
'last_name',
'address',
'delivery_address',
'phone_number',
'mobile_number',
'email_address',
'join_date',
'subscription_type',
'image'
).get()
return { 'info' : context }
First of all, when I ommit the .get() method I cannot iterate over the above query, I just get an empty queryset when doing this:
{% for key, value in info.items %}
<h3>{{ key }}:</h3> <p> {{ value }} </p>
{% empty %}
<p> no data </p>
{% endfor %}
I guess this has something to do with it not being a dictionary without the .get() method. But in the documentation it seems that it should return a iterable, I might be using it wrong, any suggestions?
Second question is:
When I try to to a "backwards relation" in the template, like so:
{% for services in info.customerhistory_set.all %}
{{ services }}
{% endfor %}
It also winds up empty.
It might be that I'm interpreting the documentation wrong but I've been googling on this subject and tried several different combinations. Any help?
Thanks
Johan
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home