Snippet List
**Your model:**
class TicketItem(models.Model):
hours = models.DecimalField(decimal_places=2, max_digits=6)
day = models.DateField()
order = models.ForeignKey(Order)
tickets = models.ManyToManyField(Ticket)
Now you want to auto save m2m fields in your forms.TicketItemCreateForm:
1. inherit from m2mForm-Class
2. define m2m_field(s)
**Example:**
class TicketItemCreateForm(m2mForm):
m2m_field = 'tickets'
class Meta:
model = models.TicketItem
- forms
- m2m
- class
- modelform
If you are like me and you find yourself often using M2M fields for tons of other on-model methods, in templates, and views alike, try using this quick and dirty caching. I show the use of a "through" model for the m2m, but that is purely optional. For example, let's say we need to do several different things with our list of beads, the old way is...
# views.py
necklace = Necklace.objects.get(id=1)
bead = Bead.objects.get(id=1)
if bead in necklace.beads.all():
# this bead is here!
# template necklace.html
{% for bead in necklace.beads.all %}
<li>{{ bead }}</li>
{% endfor %}
...which would hit the database twice. Instead, we do this:
# views.py
necklace = Necklace.objects.get(id=1)
bead = Bead.objects.get(id=1)
if bead in necklace.get_beads():
# this bead is here!
# template necklace.html
{% for bead in necklace.get_beads %}
<li>{{ bead }}</li>
{% endfor %}
Which only does one hit on the database. While we could have easily set the m2m query to a variable and passed it to the template to do the same thing, the great thing is how you can build extra methods on the model that use the m2m field but share the cache with anyone down the line using the same m2m field.
I'm by no means an expert, so if there is something here I've done foolishly, let me know.
I have a ModelForm which includes m2m field to images. User can upload images and crop them with my cool jquery cropper, then areas are saved as images and their IDs and thumbnail URLs are passed back to page and included as thumbnails with hidden inputs. I have no problem while form have no errors, and when it does, i can not just simply display thumbnails — all I have is IDs, and form has no objects to iterate cause instance was not saved, and as it was not save it has no id and as it has no id it can not have m2m relations. So i wrote templatetag which returns queryset based on ids. It works like that:
<ul id="lot-images" class="thumb-uploaders">
{% if form.errors %}
{% load load_form_objects %}
{% load_form_objects lot_form.images as images %}
{% for image in images %}
{% if image %}
<li>
<input type="hidden" name="images" value="{{image.id}}"/>
<div class="image">
<div class="mask"></div>
<img src="{{ image.get_thumbnail_url }}" alt=""/>
<a href="#" class="delete">Удалить</a>
</div>
</li>
{% else %}
<li><a class="upload-medium"></a></li>
{% endif %}
{% endfor %}
{% else %}
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
<li><a class="upload-medium"></a></li>
{% endif %}
</ul>
This snippet should allow you to do queries not before possible using Django's ORM. It allows you to "Split" up the m2m object you are filtering on.
This is best described by example:
Suppose you have `Article` and `Tag`, where `Article` has a m2m relation with `Tag` (`related_name = 'tag'`). If I run:
from django.db.models.query import Q
Article.objects.filter(Q(tag__value = 'A') & Q(tag__value = 'B'))
> # no results
I would expect to get no results (there are no tags with both a value of 'A' and 'B'). However, I *would* like to somehow get a list of articles with tags meeting that criteria. Using this snippet, you can:
from django.db.models.query import Q
from path.to.qsplit import QSplit
Article.objects.filter(QSplit(Q(tag__value = 'A')) & QSplit(Q(tag__value = 'B')))
> # articles with both tags
So now they are split into different `Tag` entries.
Notice how the `QSplit()` constructor takes a `Q` object---it's possible to give this any complicated Q-type expression.
6 snippets posted so far.