Django - Display imagefield in ManytoMany form instead of title -
i working on django project crispy forms. want use images instead of the default models title/label select instance in many many relation form.
content models.py:
class cloth(models.model): owner = models.foreignkey(settings.auth_user_model) title = models.charfield(max_length=200) picture = imagecropfield(upload_to='cloth_pics/%y-%m-%d/', blank=true) def __str__(self): return self.title class outfit(models.model): owner = models.foreignkey('profiles.profile') title = models.charfield(max_length=200) cloths=models.manytomanyfield(cloth)
content forms.py
class clothform(forms.modelform): class meta: model = cloth fields = ('title','type','picture') class outfitform(forms.modelform): class meta: model = outfit exclude= ["owner"]
content views.py
def outfits_new(request): if request.method == "post": form = outfitform(request.post) if form.is_valid(): outfit = form.save(commit=false) outfit.owner = get_user(request) outfit.created_date = timezone.now() outfit.save() pk=outfit.id return httpresponseredirect(reverse('outfit_edit_delete', args=[pk])) else: cloths = cloth.objects.filter(owner=request.user.id) form = outfitform() return render(request, '../templates/outfits_new.html', {'form': form, "cloths":cloths})
content outfits_new.html
<form enctype="multipart/form-data" method="post"> {% csrf_token %} {{ form|crispy }} <div class="btn-group" role="group" aria-label="basic example"> <input type="submit" value="submit" name="edit" class="btn btn-success"> </div>
this code produces outfit form can select different cloths( displaying cloths title). want select different cloths using image cloths.picture field.
thank much, patrick
have @ select2 @ https://select2.github.io/examples.html. allows images in comboboxes
there django package @ https://github.com/applegrew/django-select2
Comments
Post a Comment