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