I remodel my objects using ManyToMany relationship using "through" as it's guided here:
link text
class Receipt(models.Model):
name = models.CharField(max_length=128)
(...)
components = models.ManyToManyField(Product, through='ReceiptComponent')
class Admin:
pass
def __unicode__(self):
return self.name
def url(self):
return self.id
class ReceiptComponent(models.Model):
product = models.ForeignKey(Product)
receipt = models.ForeignKey(Receipt)
quantity = models.FloatField(max_length=9)
unit = models.ForeignKey(Unit)
class Admin:
pass
def __unicode__(self):
return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive
It looks ok, but I have 2 problems with it:
1) In admin management panel there's no easy connection with receipt = If I have to add a new component - I should go to components and make component connected to receipt - maybe it's the only solution - but it would be more intuitive in receipts
2) I can't print it using templates:
views.py:
(...)
def detail(request, receipt_id):
receipt = get_object_or_404(Receipt, pk=receipt_id)
components = receipt.components.all()
return render_to_response('receipt.html',{'receipt' : receipt, 'components' : components,}
(...)
receipt.html:
<h1>{{ receipt.name }}</h1>
{% for component in components.all %}
<div class='component'>{{ component }}</div>
{% endfor %}
What you did with .all is exactly what I meant -- you had it initially in 2 places, .all() in view and .all in template.
The reason of your 'error' is quite evident - components is the m2m field to Product. This is what your wrote in your code. This components is the set of products, not intermediary model ReceiptComponent.
UPD: simply leave your models as are, and use receiptcomponent_set from the Receipt
1) Have you tried inlines?
2) remove .all in your template, you don't need it.
May be you will also need
components = list(receipt.components.all())
1) looks perfect! (hint for other users: inline != inlines ;D
2) removing .all causes an exception:
Caught an exception while rendering: 'ManyRelatedManager' object is not iterable
but I understand that for good quality code I should move it from template to code:
views.py:
def detail(request, receipt_id):
receipt = get_object_or_404(Receipt, pk=receipt_id)
components = receipt.components.all()
additionals = receipt.additionals.all()
return render_to_response('drinkbook/receipts/receipt.html',{'receipt' : receipt, 'components' : components, 'additionals' : additionals, })
template:
h1>{{ receipt.name }}</h1>
{% for component in components %}
<div class='component'>{{ component }}</div>
{% endfor %}
{% if receipt.additionals %}Ponadto:
{% for additional in additionals %}
<div class='additional'>{{ additional }}</div>
{% endfor %}
{% endif %}
<p>{{ receipt.desc|safe }}</p>
Ok. It works now, but the result of component is Product.unicode not ReceiptComponent.unicode (which is a child of Product). Why?
Related
I have a class called Features in my models.py. In my html, I am displaying a list on the right that excludes two of these Features, one is the active feature that has been selected, the other is the most recently added since they are the main content of my page. The remaining Features in the list are displayed by date and do show what I am expecting.
Now, I want to single out the first, second and third Features (title only) in THAT list so I can place them in their own separate divs - because each has unique css styling. There are probably numerous ways of doing this, but I can't seem to figure any of them out.
This is a link to my project to give a better idea of what I want (basically trying to get the content in those colored boxes on the right.)
I'm just learning Django (and Python really), so thanks for your patience and help!
HTML
{% for f in past_features %}
{% if f.title != selected_feature.title %}
{% if f.title != latest_feature.title %}
<h1>{{ f.title }}</h1>
{% endif %}
{% endif %}
{% endfor %}
VIEWS
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
past_features = Feature.objects.order_by('-pub_date')
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
MODELS
class Feature(models.Model):
title = models.CharField(db_index=True, max_length=100, default='')
content = models.TextField(default='')
pub_date = models.DateTimeField(db_index=True, default=datetime.now, blank=True)
def __str__(self):
return self.title
def __iter__(self):
return [
self.id,
self.title ]
You can either store the first three Features in separate variables in your context or add checks to your template loop like {% if forloop.first %} or {% if forloop.counter == 2 %}.
If all you want is to not have the
selected_feature
latest_feature
these two records out of the past_features queryset, then you can use exclude on the past_features query and pass the id's of the selected_features and latest_feature objects.
The views.py would look like:
def feature_detail(request, pk):
selected_feature = get_object_or_404(Feature, pk=pk)
latest_feature = Feature.objects.order_by('-id')[0]
# Collect all the id's present in the latest_feature
excluded_ids = [record.pk for record in latest_feature]
excluded_ids.append(selected_feature.pk)
#This would only return the objects excluding the id present in the list
past_features = Feature.objects.order_by('-pub_date').exclude(id__in=excluded_ids)
test = Feature.objects.last()
context = {'selected_feature': selected_feature,
'latest_feature': latest_feature,
'past_features': past_features,
'test': test}
return render(request, 'gp/feature_detail.html', context)
Django provides a rich ORM and well documented, go through the Queryset options for further information.
For access to a specific object in Django templates see following example:
For access to first object you can use {{ students.0 }}
For access to second object you can use {{ students.1 }}
For access to a specific field for example firstname in object 4 you can use {{ students.3.firstname }}
For access to image field in second object you can use {{ students.1.photo.url }}
For access to id in first object you can use {{ students.0.id }}
Lets say I have a model with 2 fields:
class Bands(models.Model):
id = models.IntegerField(db_column='ID', primary_key=True)
name = models.CharField(db_column='NAME')
type = models.CharField(db_column='TYPE')
...
What I want to do is to list all the fields data with just one template. For example:
{% block content %}
{{ field_name }}
<ul>
{% for band in bands %}
<li>{{ band }}</li>
{% endfor %}
</ul>
{% endblock %}
So, how should I make my url?
url(r'^band/$', views.band, name='band')
or
url(r'^band/(?P<band>\w+)/$', views.band, name='band')
The link to that page would be:
Name
In the view I'm taking the values as this:
def band(request, field):
results = Results.objects.all()
names = [n.name for n in results]
types = [t.type for t in results]
if field == 'name':
bands = names
else:
bands = types
return render(request, 'band.html', {'bands': bands, 'field_name': field})
Is this the right way to do this (in the view and the url)? Thanks in advance.
Well, the simplest thing to do is use the DetailView.
from .models import Band
class BandDetailView(DetailView):
model = Band
And in urls.py something like:
from band.views import BandDetailView
url(r'^band/(?P<pk>\d+)/?$', BandDetailView.as_view(), name='band-detail')
And in the template:
{% url 'band-detail' pk=1 %}
That said, your model doesn't make much sense to me, as does the Led Zeppelin vs. Deep Purple bits in the view. Can you explain your project / need a bit more?
In the Django-Admin you have the possibility to define list_filter on fields of the model. This is working for ManyToMany-Fields as well.
class ModelA(models.Model):
name = models.CharField(max_length=100, verbose_name="Name")
class ModelB(models.Model):
model_a_relation = models.ManyToManyField(ModelA)
class ModelBAdmin(ModelAdmin):
list_filter = [model_a_relation, ]
admin.site.register(ModelB, ModelBAdmin)
Now, I can filter my list of elements of ModelB by relation to ModelA in the Admin object_list of ModelB.
Now my question: Is it possible to filter by multiple objects of ModelA?
In the change_view of ModelB I use django-autocomplete-light to define relations. Can I use this widget to filter in change_list, too?
I imagine the query in the background of this filter like ModelB.objects.filter(model_a_relation__in=names), where names is a list of the chosen objects of ModelA.
Thanks, Horst
I made a really dirty try to solve my issue. It works and I want to share it with you.
For better understanding I use new Models in this example:
class Tag(models.Model):
name = models.CharField(max_length=100, verbose_name="Name")
class Book(models.Model):
tags = models.ManyToManyField(Tag)
read = models.BooleanField()
class BookAdmin(ModelAdmin):
list_filter = ['read', ]
admin.site.register(Book, BookAdmin)
At first, I overwrote the changelist_view of the BookAdmin.
def changelist_view(self, request, extra_context=None):
extra_context = extra_context if extra_context else {}
q = request.GET.copy()
tags = Tag.objects.all().values('id', 'name')
current_tags = q.get('tags__id__in', [])
tag_query = request.GET.copy()
if current_tags:
tag_query.pop('tags__id__in')
current_tags = current_tags.split(',')
all_tag = False
else:
all_tag = True
for tag in tags:
if str(tag['id']) in current_tags:
tag['selected'] = True
temp_list = list(current_tags)
temp_list.remove(str(tag['id']))
tag['tag_ids'] = ','.join(temp_list)
else:
tag['selected'] = False
tag['tag_ids'] = ','.join(current_tags)
extra_context['tag_query'] = '?' if len(tag_query.urlencode()) == 0 else '?' + tag_query.urlencode() + '&'
extra_context['all_tag'] = all_tag
extra_context['tags'] = tags
return super(BookAdmin, self).changelist_view(request, extra_context=extra_context)
As you can see, I look in GET, whether there some Tags are chosen or not. Then I build new GET-Parameter for each possible Tag.
And then there is my overwriten change_list.html
{% extends "admin/change_list.html" %}
{% block content %}
{{ block.super }}
<h3 id="custom_tag_h3"> Fancy Tag filter</h3>
<ul id="custom_tag_ul">
<li{% if all_tag %} class="selected"{% endif %}>
All
</li>
{% for tag in tags %}
<li{% if tag.selected %} class="selected"{% endif %}>
{{ tag.name }}
</li>
{% endfor %}
</ul>
<script type="text/javascript">
$('#changelist-filter').append($('#custom_tag_h3'));
$('#custom_tag_h3').after($('#custom_tag_ul'));
</script>
{% endblock content %}
This way I have a filter looking like the boolean read-filter where I can activate more than one option. By clicking on the filter, a new id is added to the query. Another click on already selected option removes id from query. Click on All removes the hole tags_in-parameter from URL.
Another option to allow users to use an autocomplete in the changelist view:
setup ModelAdmin.search_fields to search in the fields served by the Autocomplete, ie.
class ModelBAdmin(ModelAdmin):
search_fields = ['model_a_relation__name']
override the changelist tomplate to spawn an autocomplete on the search input:
<script type="text/javascript">
$(document).ready(function() {
$('#searchbar').yourlabsAutocomplete({
url: '{% url 'autocomplete_light_autocomplete' 'YourAutocompleteName' %}',
choiceSelector: '[data-value]',
}).input.bind('selectChoice', function(e, choice, autocomplete) {
$(this).val(choice.text())
.parents('form').submit();
});
});
</script>
ensure that there is no JS error after adding this script !
When a user registers for my app.I receive this error when he reaches the profile page.
The 'image' attribute has no file associated with it.
Exception Type: ValueError
Error during template rendering
In template C:\o\mysite\pet\templates\profile.html, error at line 6
1 <h4>My Profile</h4>
2
3 {% if person %}
4 <ul>
5 <li>Name: {{ person.name }}</li>
6 <br><img src="{{ person.image.url }}">
Traceback Switch back to interactive view
File "C:\o\mysite\pet\views.py" in Profile
71. return render(request,'profile.html',{'board':board ,'person':person})
I think this error happens because my template requires a image and seen he just registered he can't add a image unless he go to the edit page and adds a page then he can access the profile page.
My profile.html
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
<br><img src="{{ person.image.url }}">
</ul>
{% endif %}
My Profile function at views.py
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person})
I tried this solution by creating a 2 instance of Person object and separating them at my template with a if but it didn't succeed.
<h4>My Profile</h4>
{% if person %}
<ul>
<li>Name: {{ person.name }}</li>
</ul>
{% endif %}
{% if bob %}
<ul>
<br><img src="{{ bob.image.url }}">
</ul>
My solutions to the Profile function
def Profile(request):
if not request.user.is_authenticated():
return HttpResponseRedirect(reverse('world:LoginRequest'))
board = Board.objects.filter(user=request.user)
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
return render(request,'profile.html',{'board':board ,'person':person,'bob':bob})
I'm been reading the documentation for Built-in template tags and filters I think a solution here is to use ( and ) template tag but I can't seem to use it properly.
How can I configure this template to make picture an option. If their are no picture leave it but display the persons name.
Thank you for helping me
bob and person are the same object,
person = Person.objects.get(user=request.user)
bob = Person.objects.get(user=request.user)
So you can use just person for it.
In your template, check image exist or not first,
{% if person.image %}
<img src="{{ person.image.url }}">
{% endif %}
The better approach which would not violate DRY is to add a helper method to the model class like:
#property
def image_url(self):
if self.image and hasattr(self.image, 'url'):
return self.image.url
and use default_if_none template filter to provide default url:
<img src="{{ object.image_url|default_if_none:'#' }}" />
My dear friend, others solvings are good but not enough because If user hasn't profile picture you should show default image easily (not need migration). So you can follow below steps:
Add this method to your person model:
#property
def get_photo_url(self):
if self.photo and hasattr(self.photo, 'url'):
return self.photo.url
else:
return "/static/images/user.jpg"
You can use any path (/media, /static etc.) but don't forget putting default user photo as user.jpg to your path.
And change your code in template like below:
<img src="{{ profile.get_photo_url }}" class="img-responsive thumbnail " alt="img">
Not exactly what OP was looking for, but another possible solution would be to set a default value for ImageField:
class Profile(models.Model):
# rest of the fields here
image = models.ImageField(
upload_to='profile_pics/',
default='profile_pics/default.jpg')
you have two choices :
first one:
in the model field try to put a default image value , like this :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True , default='profile_img/925667.jpg')
the second one (recommended) :
add a custom method inside your class model like the following , to return PRF_image url if exist or return empty string if not :
PRF_image = models.ImageField(upload_to='profile_img', blank=True, null=True )
#property
def my_PRF_image(self):
if self.PRF_image :
return self.PRF_image.url
return ''
and inside your template you can use :
{{ your_object.my_PRF_image }}
i hope this helpful .
You can also use the Python 3 built-in function getattr to create your new property:
#property
def image_url(self):
"""
Return self.photo.url if self.photo is not None,
'url' exist and has a value, else, return None.
"""
if self.image:
return getattr(self.photo, 'url', None)
return None
and use this property in your template:
<img src="{{ my_obj.image_url|default_if_none:'#' }}" />
Many way to solve this issue
Try below code
models.py # under Person class
#property
def imageURL(self):
if self.image:
return self.image.url
else:
return 'images/placeholder.png'
html file
<img src="{% static person.imageURL %}" class="thumbnail" />
Maybe this helps but my database didn't save on of the pictures for the object displayed on the page.
As that object in models.py has blank=False and also I am looping through object, it constantly gave an error until I added a replacement picture in the admin for the database to render.
This error also arises when any one or more items doesn't have an image added and the rest items do. To fix this:
class Product(models.Model):
pname = models.CharField(max_length=30)
price = models.IntegerField()
img = models.ImageField(null = True,blank = True)
def __str__(self):
return self.pname
#property
def imageURL(self):
try:
url = self.img.url
except:
url=''
return url
I had a similar problem , but my issue was with the form in HTML template. if you don't set the enctype = "multipart/form-data" attribute then it does not upload the image hence the reason for the error
I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.
I have a template that I want to fill with information regarding developers working on a project.
Each developer will have the following information to display:
type
title
first_name
last_name
skills
The trouble I'm running into is that each developer has many skills associated with them.
I've created the model like this:
class DevSkills(models.Model):
dev = models.ForeignKey(User)
skill = models.CharField(max_length = 200)
I'm trying to create a view that will populate the dictionary so that I can loop through each developer, display their info, then loop through each skill (to display them one at a time).
Here is the view:
def developers(request):
"""
.. function:: developers()
Provide the page that shows the developer credentials
:param request: Django Request object
"""
devs = User.objects.filter(is_staff = True)
dev_info = {}
for dev in devs:
dev_info.update(createDevDisplayDict(dev))
data = { 'user' : request.user }
data.update({ 'devs' : dev_info })
return render_to_response("developers.html", data)
I've designated the is_staff field from User to indicate the user is a developer.
I've created a simple utility that helps me populate the embedded dictionaries so I can loop through them:
def createDevDisplayDict(user):
"""
.. function:: createDevDisplayDict()
Create a dictionary for showcasing the developer
:param user: developer who we are working with
"""
userProfile = UserProfile.objects.get(user = user)
devSkills = DevSkills.objects.filter(dev = user)
dev_dict = {}
user_dict = { 'dev_type' : userProfile.dev_type,
'title' : userProfile.title,
'first_name' : user.first_name,
'last_name' : user.last_name,
}
dev_dict.update(user_dict)
skill_dict = {}
for skill in devSkills:
skill_dict.upate({ 'skill' : skill.skill })
dev_dict.update(skill_dict)
return dev_dict
My intention is to loop through each developer, create a "super" dictionary to contain each of their user_dict dictionaries (which are based on their User info) and add to that a dictionary for each of their skills. Then, back in the template I want to loop through the "super" dictionary in such a way that it will present them something like the following:
James Taylor
Project Lead
Software Developer
• Django
• Python
• JavaScript
• JQuery
Elizabeth Norton
Design Lead
Graphic Designer
• Edge
• Adobe Photoshop
• Adobe Illustrator
• CSS
Here is the template I'm trying to work with:
{% extends "base.html" %}
{% block content %}
<div>
<p>Our Developers</p>
</div>
{% for dev in devs %}
{{ dev.user_dict.first_name }} {{ dev.user_dict.last_name }}
{{ dev.user_dict.title }}
{{ dev.user_dict.dev_type }}
<ul>
{% for skill in dev.skill_dict %}
<li>skill.skill</li>
{% endfor %}
</ul>
{% endfor %}
{% endblock %}
When I see the page now it looks like this:
Our Developers
Yeah...nothing is getting populated. Any suggestions?
UPDATE 1:
I've modified my utility per iMom0's suggestion. I'm now using a list to contain each skill. Like so:
def createDevDisplayDict(user):
"""
.. function:: createDevDisplayDict()
Create a dictionary for showcasing the developer
:param user: developer who we are working with
"""
userProfile = UserProfile.objects.get(user = user)
devSkills = DevSkills.objects.filter(dev = user)
dev_dict = {}
user_dict = { 'dev_type' : userProfile.dev_type,
'title' : userProfile.title,
'first_name' : user.first_name,
'last_name' : user.last_name,
}
dev_dict.update(user_dict)
skills = []
for skill in devSkills:
skills.append(skill.skill)
skill_dict = {'skill' : skills}
dev_dict.update(skill_dict)
return dev_dict
I can see the value in doing this - in fact, it's much more intuitive and I think I was making it too hard the other way. But my template still shows up bare. :(
UPDATE 2:
I know I'm on the write path now. I put some logging in the view:
devs = User.objects.filter(is_staff = True, is_superuser = False)
dev_info = {}
for dev in devs:
dev_info.update(createDevDisplayDict(dev))
for key in dev_info:
for sub_key in dev_info[key]:
logfile.write('{0} = {1}\n'.format(sub_key, dev_info[key][sub_key]))
And the logfile displays:
skills = [u'Java', u'Perl', u'C++', u'Python', u'Django']
dev_type = Software Developer
first_name = Rico
last_name = Cordova
title = Owner
So, it has to be a way I'm calling it in the template, right?
UPDATE 3:
I had a realization that I was disconnecting the user_dict and their skills. So I modified the utility slightly to bring them into a single dictionary.
## Create a logging object
userProfile = UserProfile.objects.get(user = user)
devSkills = DevSkills.objects.filter(dev = user)
dev_dict = {}
user_dict = { 'dev_type' : userProfile.dev_type,
'title' : userProfile.title,
'first_name' : user.first_name,
'last_name' : user.last_name,
}
skills = []
for skill in devSkills:
skills.append(skill.skill)
user_dict.update({ 'skills' : skills })
dev_dict['user_dict'] = user_dict
return dev_dict
This is a much better solution, in my opinion. I'm still having trouble accessing the user_dict info in the template though. :(
You could be using Django's ORM features to make this a lot easier (and, we'll see, get better performance), it's a great feature!
Model code
class DevSkill(models.Model):
dev = models.ForeignKey(UserProfile, related_name = 'skill_set')
skill = models.CharField(max_length = 200)
We changed two things:
Using a UserProfile ForeignKey instead of user will simplify the rest of the code. Since you have a UserProfile <-> User mapping anyway, this is not going to be an issue.
We added a related_name attribute so that any UserProfile object now has a skill_set attribute which store it's list of DevSkills.
(Please note that related_name is not required, and Django will create a generic modelname_set attribute if you don't set it).
Also, DevSkill should be singular, the object is a single skill!
I also expect that you have the following for UserProfile, and created code assuming you did. You'll need to adapt if you don't.
class UserProfile(models.Model):
user = models.OneToOneField(User)
title = models.CharField(max_length = 40)
dev_type = # W/E you want
In the view:
devs = UserProfile.objects.all() # Or W/E queryset would fit.
# Pass context & all.
In the template:
{% extends "base.html" %}
{% block content %}
<div>
<p>Our Developers</p>
</div>
{% for dev in devs %}
{{ dev.user.first_name }} {{ dev.user.last_name }}
{{ dev.title }}
{{ dev.dev_type }}
<ul>
{% for skill in dev.skill_set.all %}
<li>skill.skill</li>
{% endfor %}
</ul>
{% endfor %}
{% endblock %}
Performance
Please be aware that this code (the one you're using now too, though) is going to absolutely kill performance. Indeed, we're doing several queries for each user (Hitting the database for their User and their DevSkills).
That's not a problem though, we can use the ORM's select_related and prefetch_related features to solve that issue:
devs = UserProfile.objects.select_related('user').prefetch_related('skill_set').all()
That way, we only do two queries, one for the UserProfile -> Userand one for the DevSkill's, for which the joining is done in Python, but you shouldn't care about that, Django does it for you.
Please be aware that prefetch_related is a Django 1.4 feature.
Footnote: the UserProfile stuff is going away in Django 1.5, check it out!
dict.update always overwrite the value of dict
In [2]: d = {'key': 'value'}
In [3]: d.update({'key': 'value1'})
In [4]: d
Out[4]: {'key': 'value1'}
Instead, you should use list and list.append.
And your template do not know what user_dict is, correct it,
dev_dict['user_dict'] = user_dict
This is a new concept for me - dict.items in a template. The following is exactly how I was able to display what I wanted.
{% extends "base.html" %}
{% block content %}
<div>
<p>Our Developers</p>
</div>
{% for key, value in devs.items %}
{{ value.first_name }} {{ value.last_name }} <br>
{{ value.title }} <br>
{{ value.dev_type }} <br>
<ul>
{% for skill in value.skills %}
<li>{{ skill }}</li>
{% endfor %}
</ul>
{% endfor %}
{% endblock %}