I want to list only usable items in OneToOneField not all items, its not like filtering values in ChoiceField because we need to find out only values which can be used which is based on the principle that whether it has been used already or not.
I am having a model definition as following:
class Foo(models.Model):
somefield = models.CharField(max_length=12)
class Bar(models.Model):
somefield = models.CharField(max_length=12)
foo = models.OneToOneField(Foo)
Now I am using a ModelForm to create forms based on Bar model as:
class BarForm(ModelForm):
class Meta:
model = Bar
Now the problem is in the form it shows list of all the Foo objects available in database in the ChoiceField using the select widget of HTML, since the field is OneToOneField django will force to single association of Bar object to Foo object, but since it shows all usable and unusable items in the list it becomes difficult to find out which values will be acceptable in the form and users are forced to use hit/trial method to find out the right option.
How can I change this behavior and list only those items in the field which can be used ?
Although this is an old topic I came across it looking for the same answer.
Specifically for the OP:
Adjust your BarForm so it looks like:
class BarForm(ModelForm):
class Meta:
model = Bar
def __init__(self, *args, **kwargs):
super(BarForm, self).__init__(*args, **kwargs)
#only provide Foos that are not already linked to a Bar, plus the Foo that was already chosen for this Bar
self.fields['foo'].queryset = Foo.objects.filter(Q(bar__isnull=True)|Q(bar=self.instance))
That should do the trick. You overwrite the init function so you can edit the foo field in the form, supplying it with a more specific queryset of available Foo's AND (rather important) the Foo that was already selected.
For my own case
My original question was: How to only display available Users on a OneToOne relation?
The Actor model in my models.py looks like this:
class Actor(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name = 'peactor')
# lots of other fields and some methods here
In my admin.py I have the following class:
class ActorAdmin(admin.ModelAdmin):
# some defines for list_display, actions etc here
form = ActorForm
I was not using a special form before (just relying on the basic ModelForm that Django supplies by default for a ModelAdmin) but I needed it for the following fix to the problem.
So, finally, in my forms.py I have:
class ActorForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ActorForm, self).__init__(*args, **kwargs)
#only provide users that are not already linked to an actor, plus the user that was already chosen for this Actor
self.fields['user'].queryset = User.objects.filter(Q(peactor__isnull=True)|Q(peactor=self.instance))
So here I make an ActorForm and overwrite the __init__ method.
self.fields['user'].queryset =
Sets the queryset to be used by the user formfield. This formfield is a ModelChoiceField
by default for a OneToOneField (or ForeignKey) on a model.
Q(peactor__isnull=True)|Q(peactor=self.instance)
The Q is for Q-objects that help with "complex" queries like an or statement.
So this query says: where peactor is not set OR where peactor is the same as was already selected for this actor
peactor being the related_name for the Actor.
This way you only get the users that are available but also the one that is unavailable because it is already linked to the object you're currently editing.
I hope this helps someone with the same question. :-)
You need something like this in the init() method of your form.
def __init__(self, *args, **kwargs):
super(BarForm, self).__init__(*args, **kwargs)
# returns Bar(s) who are not in Foo(s).
self.fields['foo'].queryset = Bar.objects.exclude(id__in=Foo.objects.all().values_list(
'bar_id', flat=True))
PS: Code not tested.
Related
I have a (horrible) database table that will be imported from a huge spreadsheet. The data in the fields is for human consumption and is full of "special cases" so its all stored as text. Going forwards, I'd like to impose a bit of discipline on what users are allowed to put into some of the fields. It's easy enough with custom form validators in most cases.
However, there are a couple of fields for which the human interface ought to be a ChoiceField. Can I override the default form field type (CharField)? (To clarify, the model field is not and cannot be constrained by choices, because the historical data must be stored. I only want to constrain future additions to the table through the create view).
class HorribleTable( models.Model):
...
foo = models.CharField( max_length=16, blank=True, ... )
...
class AddHorribleTableEntryForm( models.Model)
class Meta:
model = HorribleTable
fields = '__all__' # or a list if it helps
FOO_CHOICES = (('square', 'Square'), ('rect', 'Rectangular'), ('circle', 'Circular') )
...?
Perhaps you could render the forms manually, passing the options through the context and make the fields in html.
Take a look at here:https://docs.djangoproject.com/en/4.0/topics/forms/#rendering-fields-manually
I think you can easily set your custom form field as long it will match the data type with the one set in your model (e.g. do not set choices longer than max_length of CharField etc.). Do the following where foo is the same name of the field in your model:
class AddHorribleTableEntryForm(forms.ModelForm):
foo = forms.ChoiceField(choices=FOO_CHOICES)
class Meta:
model = HorribleTable
...
I think this is perfectly fine for a creation form. It's will not work for updates as the values in the DB will most probably not match your choices. For that, I suggest adding a second form handling data updates (maybe with custom permission to restrict it).
UPDATE
Another approach will be to override the forms init method. That way you can handle both actions (create and update) within the same form. Let the user select from a choice field when creating an object. And display as a normal model field for existing objects:
class AddHorribleTableEntryForm(forms.ModelForm):
foo = forms.ChoiceField(choices=FOO_CHOICES)
class Meta:
model = HorribleTable
fields = '__all__' # or a list if it helps
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
instance = kwargs.get("instance", None)
if instance is None:
self.fields["foo"].widget = forms.widgets.Select(choices=self.FOO_CHOICES)
I have a model field with choices:
class MyModel(models.Model):
myfield = models.CharField(max_length=1000, choices=(('a','a'),('b','b'))
I know that I can access in forms this specific field and override its choices option like that:
self.instance._meta.get_field(field_name).choices = (('c','c'),('d','d'))
but that will change the choices for the entire model, not for an individual instance. What is the correct way to do it for one specific instance only or it is not possible?
I'm not aware of any way to change the model's field choices on a per-instance basis, but if it's for a form you can override the form's field choices (example written from memory so it might no be 100% accurate):
class MyModelForm(forms.ModelForm):
class Meta:
model = MyModel
def __init__(self, *args, **kw):
super(MyModelForm, self).__init__(*args, **kw)
if some_condition:
self.fields["myfield"].choices = (...)
Important: you want to override self.fields["myfield"], not self.myfield - the latter is a class attribute so changing it would affect all MyModelForm instances for the current process, something you won't usually notice when running the dev server but that will cause very erratic behaviour on production.
I use abstract models in Django like:
class Tree(models.Model):
parent = models.ForeignKey('self', default=None, null=True, blank=True,
related_name="%(app_label)s_%(class)s_parent")
class Meta:
abstract = True
class Genre(Tree):
title = models.CharField(max_length=150)
And all fields from the abstract model go first in Django's admin panel:
parent:
abstract_field2:
title:
model_field2:
...
Is there a way to put them (fields from abstract classes) in the end of the list?
Or a more general way to define order of fields?
You can order the fields as you wish using the ModelAdmin.fields option.
class GenreAdmin(admin.ModelAdmin):
fields = ('title', 'parent')
Building off rbennell's answer I used a slightly different approach using the new get_fields method introduced in Django 1.7. Here I've overridden it (the code would be in the definition of the parent's ModelAdmin) and removed and re-appended the "parent" field to the end of the list, so it will appear on the bottom of the screen. Using .insert(0,'parent') would put it at the front of the list (which should be obvious if you're familiar with python lists).
def get_fields (self, request, obj=None, **kwargs):
fields = super().get_fields(request, obj, **kwargs)
fields.remove('parent')
fields.append('parent') #can also use insert
return fields
This assumes that your fields are a list, to be honest I'm not sure if that's an okay assumption, but it's worked fine for me so far.
I know it's an old question, but wanted to thow in my two cents, since my use case was exactly like this, but i had lots of models inheriting from one class, so didn't want to write out fields for every admin. Instead I extended the get_form model and rearranged the fields to ensure parent always comes at the end of the fields in the admin panel for add/change view.
class BaseAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(BaseAdmin, self).get_form(request, obj, **kwargs)
parent = form.base_fields.pop('parent')
form.base_fields['parent '] = parent
return form
base_fields is an OrderedDict, so this appends the 'parent' key to the end.
Then, extend this admin for any classes where you want parent to appear at the end:
class GenreAdmin(BaseAdmin):
pass
This simple solution from #acquayefrank (in the comments) worked for me:
The order of fields would depend on the order in which you declare them in your models.
Let's say I have a single ModelForm which can be filled out by different tiers of users. The Admin can edit any field of that form; but for other users, I need to have certain fields pre-defined, and read-only and/or hidden.
Using my CBV's get_form_kwargs method, I have made the form aware of the user that's bringing it up, and, in its __init__ method I react accordingly, tweaking the form's exclude, and the fields' required and initial properties; and then, in my view's form_valid, I further enforce the values. But, frankly, I'm neither sure that every operation I do is actually needed, nor whether there's some gaping hole I'm not aware of.
So, what's the best, cleanest way of doing this?
Assuming there aren't a lot of combinations, I would create a different form that meets the different needs of your users. Then override def get_form_class and return the correct form based on your needs. This keeps the different use cases separate and gives flexibility if you need to change things in the future without breaking the other forms.
# models.py
class Foo(models.Model):
bar = model.CharField(max_length=100)
baz = model.CharField(max_length=100)
biz = model.CharField(max_length=100)
# forms.py
class FooForm(forms.ModelForm): # for admins
class Meta:
model = Foo
class FooForm(forms.ModelForm): # users who can't see bar
boo = forms.CharField()
class Meta:
model = Foo
exclude = ['bar']
class FooFormN(forms.ModelForm): # as many different scenarios as you need
def __init__(self, *args, **kwargs)
super(FooFormN, self).__init__(*args, **kwargs)
self.fields['biz'].widget.attrs['readonly'] = True
class Meta:
model = Foo
# views.py
class SomeView(UpdateView):
def get_form_class(self):
if self.request.user.groups.filter(name="some_group").exists():
return FooForm
# etc.
I have a django model as following
class Project(models.Model)
name=models.CharField(max_length=200)
class Application(models.Model)
proj=models.ForeignKey(Project, null=True, blank=True)
I need to modify the admin form of the project to be able to assign multiple applications to the project, so in the admin.py I have created a ModelAdmin class for the project as following
class ProjectAdmin(ModelAdmin)
form=projectForm
project_apps=[]
and the project form as following
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
project_apps =forms.ModelMultipleChoiceField(queryset=Application.objects.all(),required=False,)
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
if self.instance.id is not None:
selected_items = [ values[0] for values in Application.objects.filter(project=self.instance) ]
self.fields['project_apps'].initial = selected_items
def save(self,commit=True):
super(ProjectForm,self).save(commit)
return self.instance
by doing this I have a multiple select in the create/edit project form.
what I need is to override the save method to save a reference for the project in the selected applications?
how can I get the selected applications ????
Not entirely sure what you're trying to do, but maybe this?
def save(self,commit=True):
kwargs.pop('commit') # We're overriding this with commit = False
super(ProjectForm,self).save(commit)
if self.instance:
for a in self.cleaned_data['project_apps']:
a.proj = self.instance
a.save()
return self.instance
Now, I can't remember if in this case, self.cleaned_data['project_apps'] will actually contain a list of Application objects or not. I suspect it will, but if not this function will take care of that:
def clean_project_apps(self):
app_list = self.cleaned_data['project_apps']
result = []
for a in app_list:
try:
result.append(Application.objects.get(pk=a)
except Application.DoesNotExist:
raise forms.ValidationError("Invalid application record") # to be safe
return result
All in all I think this form is a bad idea though, because basically what is happening here is you're displaying all of the application records which doesn't make sense, since most of them will be associated with other projects.
Oh oh oh!!! Just noticed you wanted this to show up in a Multiple Select list!
You're (probably) doing it wrong
A multiple select means this isn't a one-to-many relationship. It's a many-to-many relationship.
This is what you want to do, easy peasy, doesn't require any custom forms or anything.
class Project(models.Model)
name=models.CharField(max_length=200)
project_apps = models.ManyToMany('Application', null=True, blank=True)
class Application(models.Model)
# nothing here (NO foreign key, you want more than one App/Proj and vice versa)
Indicating that this is a many-to-many field in Project will automagically create the multiple select box in admin. Ta da!