I am writing a custom widget for multiple image uploads. My models are:
models.py
class Room(models.Model):
....
....
class Picture (models.Model):
room = models.ForeignKey(Room)
url=models.ImageField(upload_to='slider', height_field=None, width_field=None, max_length=100)
def __unicode__(self):
return str(self.url)
I want to create custom widget which allow multiple image upload to be shown on rooms form
This is what I tried so far:
forms.py
class MultyImageWidget(forms.Widget):
....
def render(self, name, value, attrs=None):
context = {
'images': Picture.objects.filter(room = *room_id_of_currently_edited_room*)
# OR
# Any another way to get set of images from pictures table
}
return mark_safe(render_to_string(self.template_name, context))
class RoomsForm(forms.ModelForm):
gallery = forms.ImageField(widget=MultyImageWidget, required=False)
class Meta:
model = Room
fields = '__all__'
So problem is, I don't have gallery field in room model but I want to use widget to manage pictures which is stored in picture table similar to how one can manage data through inlines.
How to get id of room which is currently edited from my widget?
Or is there any other way to get related pictures?
Thanks
I believe you are using a wrong approach to your problem. Widget should only be responsible of displaying data and should not care about where that data come from. your render method accepts a value parameter, which is used to pass data to widget, so your render should look similar to this:
class MultyImageWidget(forms.Widget):
....
def render(self, name, value, attrs=None):
context = {
'images': value
}
return mark_safe(render_to_string(self.template_name, context))
Now we need to pass the needed Picture queryset. We can do this via form - and that what forms are for. You provided no context and I cannot comment yet to ask for details, but I suppose you are using a view to construct this form. If it is true, let's say we have some view, then we can do it this way:
def gallery_view(request, *args, **kwargs):
....
room = # get your room (maybe you pass ID via request or via arg or kwarg)
pictures = Picture.objects.filter(room=room) # or you can do room.picture_set.all()
form = RoomsForm(initial={'gallery': pictures})
....
If you are using this form with Django admin, a form has instance attribute and you can code like this:
class RoomsForm(forms.ModelForm):
gallery = forms.ImageField(widget=MultyImageWidget, required=False)
class Meta:
model = Room
fields = '__all__'
def __init__(self, *args, **kwargs):
super(RoomsForm, self).__init__(*args, **kwargs) # you may omit parameters to super in python 3
if self.instance:
self.fields['gallery'].initial = Picture.objects.filter(room=self.instance)
I hope this solves your problem
Related
My Content model has a many-to-many relationship to the Tag model. When I save a Content object, I want to add the relationships dynamically. I'm doing this the following way.
def tag_content(obj):
for tag in Tag.objects.all():
print tag
obj.tags.add(tag)
obj.is_tagged = True
obj.save()
class Tag(models.Model):
name = models.CharField(max_length=255)
class Content(models.Model):
title = models.CharField(max_length=255)
is_tagged = models.BooleanField(default=False)
tags = models.ManyToManyField(Tag, blank=True)
def save(self, *args, **kwargs):
super(Content, self).save(*args, **kwargs)
#receiver(post_save, sender = Content)
def update_m2m_relationships_on_save(sender, **kwargs):
if not kwargs['instance'].is_tagged:
tag_content(kwargs['instance'])
The tag_content function runs, however, the m2m relationships are not established. Im using Django 1.9.8 btw. This makes no sense. What am I missing? Moreover, if I do something like tag_content(content_instance) in shell, then the tags are set, so the function is ok. I guess the problem is in the receiver. Any help?
Edit
My question has nothing to do with m2m_changed, as I have said, creating a Content object in shell works perfectly. Therefore, the problem lies in the admin panel's setup.
Ok so I solved the problem. Basically, this has something to do with how Django handles its form in the admin panel. When trying to add the Contents from admin, I kept the tags field empty, thinking the tag_content function would handle it. However, that is exactly where the problem was, as creating a Content from shell tagged it just fine. In other words, changing the admin panel to something like this solved my problem :
from django.contrib import admin
from myapp.models import *
from django import forms
class ContentCreationForm(forms.ModelForm):
class Meta:
model = Content
fields = ('title',)
class ContentChangeForm(forms.ModelForm):
class Meta:
model = Content
fields = ('title', 'is_tagged', 'tags')
class ContentAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
if obj is None:
return ContentCreationForm
else:
return ContentChangeForm
admin.site.register(Tag)
admin.site.register(Content, ContentAdmin)
When trying to create a new Content, only the 'title' field is presented. This solves the problem.
My friends and I play a spreadsheet-based sports picking game which is very tedious to make changes to. I've wanted to learn Django for a while so I've been working on creating it as a webapp. Here are the models I'm working with:
class Sheet(models.Model):
user = models.ForeignKey(User)
... other stuff
class Game(models.Model):
home_team = models.CharField(max_length=100, default='---')
away_team = models.CharField(max_length=100, default='---')
... other stuff
class Pick(models.Model):
sheet = models.ForeignKey(Sheet)
game = models.ForeignKey(Game)
HOME = 'H'
AWAY = 'A'
PICK_TEAM_CHOICES = (
(HOME, 'Home'),
(AWAY, 'Away'),
)
pick_team = models.CharField(max_length=4,
choices=PICK_TEAM_CHOICES,
default=HOME)
... other stuff
Right now, I'm trying to nail down a simple way to display the following form with information from a foreign keyed model instead of the pick_team default choices. The game is hidden because it's paired with the generated PickForm via use of the initial functionality in the view.
class PickForm(ModelForm):
class Meta:
model = Pick
widgets = {'game': forms.HiddenInput()}
fields = ['sheet','game','amount','pick_type','pick_team']
def __init__(self, *args, **kwargs):
game = kwargs['initial']['game']
super(PickForm, self).__init__(*args, **kwargs)
self.fields['pick_team']=forms.ModelChoiceField([game.away_team,game.home_team])
From what I can tell, the ModelChoiceField expects a queryset- so when I provide a list or a tuple, I get a 'list' object has no attribute 'iterator' error. Knowing this now, how can I display the Game fields home_team and away_team in the pick_team dropdown on the template? Currently it defaults to 'Home' and 'Away'.
I know this is a common question at the core- how to display ForeignKeyed information in a dropdown, however all the answers I've found are fine with providing a queryset to ModelChoiceField, because they're typically trying to list a field from every object (or some filtered subset). In this case, I only want to list 2 fields, and only from one object.
I tried returning a queryset consisting of the Game object already present in kwargs, but it just displays the game's str() method in the dropdown, and attempting to refine the queryset with the relevant field names isn't working either.
EDIT: I realized that actually using the home_team and away_team values from the Game object would require extra processing on saving the Pick, or possibly be harder than that. Is there any way to do this sort of aliasing in the template alone? Similar to how with choice fields I can use get_pick_team_display to show a nicer looking display value ('Home', 'Away') instead of the vague 'H' or 'A'.
EDIT2: View code
class GameDetail(DetailView):
#model = Game
template_name = 'app/games.html'
context_object_name = 'game_detail'
def get_object(self):
game = get_object_or_404(...object filtering)
return game
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
try:
pick = Pick.objects.get(game=context['game_detail'],
....other stuff)
context['pickform'] = PickForm(initial={'game':context['game_detail'],
.... other stuff
except Pick.DoesNotExist:
#pick = none
context['pickform'] = PickForm(initial={'game':context['game_detail'],
})
return context
def post(self, request, *args, **kwargs):
form = PickForm(request.POST)
if form.is_valid():
....process form
This is a quickfix approach. In your __init__, instead of reassigning pick_team field, just redefine its options as follows:
self.fields['pick_team'].choices = (
('H', game.home_team),
('A', game.away_team),
)
I'm trying to find best approach for what i want. And i could use some help for that.
I have Model A and Model B. Model B has
modela = forms.ForeignKey(Model a)
I want to create a view where you can edit both single Model A and several Model B's on same page. Django has formsets for this and they work great.
I have one detail though, that messes things up tiny bit. Namely - i want the widgets or model B fields to be different based on what choices they have done in previous fields of same object. Because - based on type, the widget has to be datetime picker input or plain textinput.
Model B looks like this:
class ModelB(models.Model):
m0odela = models.ForeignKey(ModelA)
target_value = models.CharField()
target_type = models.CharField( choices = ( there are choices))
target_threshold = models.CharField()
I know i can provide my own form for formset and i could do this widget assignment in that form.
But the problem is, that when formset has no instances/queryset then i cant check if 'target_type' has been set for forms instance. So i would have to do it based on self.data or self.initial in form. But self.initial is also not present in form.__init__(). What i can work with is self.data - but that is raw request.POST or request.GET data - which contains all keys like 'mymodelb_set-0-target_type'.
So i'm bit lost here. Do i have to do some key parsing and figure out which -target_type belongs to current form and get chosen value there and assign widgets based on this value?
Or do i have to create my own subclass of BaseInlineFormSet and override _construc_form there somehow? So that form would have initial key with related data in **kwargs.
Has someone ran into this kind of problem before?
Alan
Well i had to solve it so i solved it as good/bad i could.
I created my own subclass of inline formset:
class MyInlineFormSet(BaseInlineFormSet):
def _construct_form(self, i, **kwargs):
initial = {}
fname = '%s-%s-%s' % (self.prefix, i, 'important_field_name')
initial['target_type'] = self.data[fname] if fname in self.data.keys() else 'km'
kwargs.update({'initial':initial})
form = super(MyInlineFormSet, self)._construct_form(i, **kwargs)
return form
And then in the form class:
class MyNiftyForm(forms.ModelForm):
class Meta:
model = MyAwesomeObject
fields=('field_one', 'field_two', 'field_three')
def __init__(self, *args, **kwargs):
super(ServiceTargetForm, self).__init__(*args, **kwargs)
if self.instance:
if self.instance.field_one == 'date':
self.fields['field_one'].widget.attrs['class'] = 'datepicker'
if self.initial:
if self.initial['field_one'] == 'date':
self.fields['field_one'].widget.attrs['class'] = 'datepicker'
and then in view:
MySuperCoolFormSet = inlineformset_factory(ImportantObject, MyAwesomeObject, extra = 1, form = MyNiftyForm, formset = MyInlineFormSet)
And it works.
Alan
I have a simple structure, with a Product model, and AppMedia model and a ProductMedia join table.
Product (has many) ProductMedia
ProductMedia (has one) AppMedia
What I would really like is to see the thumbnail for AppMedia's media field in an inline form in Product.
e.g. edit Product in Django admin, shows a StackedInline form. This contains (at present) a drop down (select) of all AppMedia.
What I need is the thumbnail.
Any help appreciated.
I am certain this isn't difficult, but I'm struggling. It's quite straight forward to put a thumbnail in for the AppMedia form (which is where the media ImageField resides), but not for the ProductMedia form which is using AppMedia as a ForeignKey.
Basic Models...
class Product(models.Model):
name = models.CharField(max_length=100)
class AppMedia(models.Model):
media = models.ImageField(upload_to=appmedia_upload_to) #appmedia_upload_to defined elsewhere)
class ProductMedia(models.Model):
title = models.CharField(max_length=150)
media = models.ForeignKey(AppMedia)
media_order = models.IntegerField(default=0)
product = models.ForeignKey(Product)
The AppMedia is shared in this way to stop multiple uploads of the same file and also to store extra meta-data (not displayed here) with the image.
I had the same issue. I hope at least one of my attempts will be usable for your case:
My first attempt to solve this problem (without overriding ForeignKeyRawIdWidget) was to:
set form property in inline admin where thumbnail should be displayed
add another field into given form class with widget, which will display a thumbnail
However I abandoned this solution because I think I have to inject data about thumbnail into given field in form's constructor and I don't consider it a nice solution.
My next solution was to use MultiWidget for some field. In this case I don't need to add another field to form and I'll have data required to display thumbnail in widget without any need to inject them in constructor.
class ThumbnailMultiWidget(MultiWidget):
def decompress(self, value):
#has to be overriden
return [None,None]
class ThumbnailWidget(Widget):
def render(self, name, value, attrs=None):
#not quite sure what is in `value`, I've not been so far
return '<img src="%s"/>' % value.url
class PhotoInlineForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(PhotoInlineForm, self).__init__(*args, **kwargs)
wdgts = [self.fields['media'].widget, ThumbnailWidget()]
self.fields['media'].widget = ThumbnailMultiWidget(widgets=wdgts)
class Meta:
model = RecipePhoto
But I abandoned this solution as well, because I found out, that there's actually an instance's representation in ForeignKeyRawIdWidget (which is widget I use) with all data I need to show the thumbnail. And that is my final solution:
So because my inline items have raw_id_field for choosing an inline record, I could simply override method label_for_value in ForeignKeyRawIdWidget, which is used to represent existing inline record. Usually it's __unicode__ (I think). I inherited ForeignKeyRawIdWidget and overriden this method to show image thumbnail:
class PhotoForeignKeyRawIdWidget(ForeignKeyRawIdWidget):
def label_for_value(self, value):
key = self.rel.get_related_field().name
try:
obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
except (ValueError, self.rel.to.DoesNotExist):
return ''
else:
"""
there's utilized sorl.thumbnail, but you can return st like this:
<img src='%s' /> % obj.media.url
"""
return Template("""{% load thumbnail %}
{% thumbnail image.image "120x120" crop="center" as one %}
<img src="{{ one.url }}" />
{% endthumbnail %}""").render(Context({
'image': obj
}))
class AppMediaInlineAdmin(admin.TabularInline):
model = AppMedia
extra = 1
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
if db_field.name == 'media':
db = kwargs.get('using')
kwargs['widget'] = PhotoForeignKeyRawIdWidget(db_field.rel, self.admin_site, using=db)
return super(AppMediaInlineAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
You should look into the related_name parameter for foreign keys, which will allow you to access the ProductMedia in reverse, i.e. if you change the ProductMedia model to:
class ProductMedia(models.Model):
title = models.CharField(max_length=150)
media = models.ForeignKey(AppMedia)
media_order = models.IntegerField(default=0)
product = models.ForeignKey(Product, related_name='media')
You could access the media object in Product model, which will allow you to put it in your admin in-line form. i.e you would have (for an easier explanation I've put the ImageField in ProductMedia):
class Product(models.Model):
name = models.CharField(max_length=100)
def admin_image(self):
return '<img src="%s"/>' % (self.media.all()[0].image.url)
admin_image.allow_tags = True
class ProductMedia(models.Model):
title = models.CharField(max_length=150)
image = models.ImageField(upload_to=appmedia_upload_to) #appmedia_upload_to defined elsewhere)
media_order = models.IntegerField(default=0)
product = models.ForeignKey(Product, related_name='media')
Then in your admin.py put:
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'admin_image')
admin.site.register(models.Product, ProductAdmin)
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.related_name
Hope I understood your question correctly and that helped. Also the code isn't tested but I'm pretty sure it should work.
I need to make a form, which have 1 select and 1 text input. Select must be taken from database.
model looks like this:
class Province(models.Model):
name = models.CharField(max_length=30)
slug = models.SlugField(max_length=30)
def __unicode__(self):
return self.name
It's rows to this are added only by admin, but all users can see it in forms.
I want to make a ModelForm from that. I made something like this:
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=CHOICES),
}
but it doesn't work. The select tag is not displayed in html. What did I wrong?
UPDATE:
This solution works as I wanto it to work:
class ProvinceForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ProvinceForm, self).__init__(*args, **kwargs)
user_provinces = UserProvince.objects.select_related().filter(user__exact=self.instance.id).values_list('province')
self.fields['name'].queryset = Province.objects.exclude(id__in=user_provinces).only('id', 'name')
name = forms.ModelChoiceField(queryset=None, empty_label=None)
class Meta:
model = Province
fields = ('name',)
Read Maersu's answer for the method that just "works".
If you want to customize, know that choices takes a list of tuples, ie (('val','display_val'), (...), ...)
Choices doc:
An iterable (e.g., a list or tuple) of
2-tuples to use as choices for this
field.
from django.forms.widgets import Select
class ProvinceForm(ModelForm):
class Meta:
CHOICES = Province.objects.all()
model = Province
fields = ('name',)
widgets = {
'name': Select(choices=( (x.id, x.name) for x in CHOICES )),
}
ModelForm covers all your needs (Also check the Conversion List)
Model:
class UserProvince(models.Model):
user = models.ForeignKey(User)
province = models.ForeignKey(Province)
Form:
class ProvinceForm(ModelForm):
class Meta:
model = UserProvince
fields = ('province',)
View:
if request.POST:
form = ProvinceForm(request.POST)
if form.is_valid():
obj = form.save(commit=True)
obj.user = request.user
obj.save()
else:
form = ProvinceForm()
If you need to use a query for your choices then you'll need to overwrite the __init__ method of your form.
Your first guess would probably be to save it as a variable before your list of fields but you shouldn't do that since you want your queries to be updated every time the form is accessed. You see, once you run the server the choices are generated and won't change until your next server restart. This means your query will be executed only once and forever hold your peace.
# Don't do this
class MyForm(forms.Form):
# Making the query
MYQUERY = User.objects.values_list('id', 'last_name')
myfield = forms.ChoiceField(choices=(*MYQUERY,))
class Meta:
fields = ('myfield',)
The solution here is to make use of the __init__ method which is called on every form load. This way the result of your query will always be updated.
# Do this instead
class MyForm(forms.Form):
class Meta:
fields = ('myfield',)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make the query here
MYQUERY = User.objects.values_list('id', 'last_name')
self.fields['myfield'] = forms.ChoiceField(choices=(*MYQUERY,))
Querying your database can be heavy if you have a lot of users so in the future I suggest some caching might be useful.
the two solutions given by maersu and Yuji 'Tomita' Tomita perfectly works, but there are cases when one cannot use ModelForm (django3 link), ie the form needs sources from several models / is a subclass of a ModelForm class and one want to add an extra field with choices from another model, etc.
ChoiceField is to my point of view a more generic way to answer the need.
The example below provides two choice fields from two models and a blank choice for each :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=([['','-'*10]]+[[x.id, x.__str__()] for x in Speakers.objects.all()]))
event = forms.ChoiceField(choices=( [['','-'*10]]+[[x.id, x.__str__()] for x in Events.objects.all()]))
If one does not need a blank field, or one does not need to use a function for the choice label but the model fields or a property it can be a bit more elegant, as eugene suggested :
class MixedForm(forms.Form):
speaker = forms.ChoiceField(choices=((x.id, x.__str__()) for x in Speakers.objects.all()))
event = forms.ChoiceField(choices=(Events.objects.values_list('id', 'name')))
using values_list() and a blank field :
event = forms.ChoiceField(choices=([['','-------------']] + list(Events.objects.values_list('id', 'name'))))
as a subclass of a ModelForm, using the one of the robos85 question :
class MixedForm(ProvinceForm):
speaker = ...