I have three different models illustrated as below and I want to be able to edit three different forms in formset.
models.py
class Parent(models.Model):
parent_name = models.CharField(max_length=20)
class Child(models.Model):
child_name = models.CharField(max_length=20)
parent = models.ForeignKey("Parent",on_delete=models.PROTECT)
birth_place = models.OneToOneField("BirthPlace", on_delete=models.PROTECT)
class BirthPlace(models.Model):
place_name = models.CharField(max_length=20)
forms.py
ChildrenFormset = inlineformset_factory(Parent, Child, fields='__all__', extra=0)
So far, I have managed to create a formset where I can work with Parent and Child. Also, in html, I have written some javascript to add a child form dynamically.
Now the problem is embedding form for BirtPlace. I have tried the below:
def add_fields(self, form, index):
super(ChildrenFormset, self).add_fields(form, index)
form.initial['birth_place_name']=form.instance.birthplace.place_name
However, it throws RelatedObjectDoesNotExist.
Can you please help?
Thanks
Related
SOLUTION AT THE BOTTOM
Problem: Django form populating with list of objects rather than values
Summary: I have 2 models Entities and Breaks. Breaks has a FK relationship to the entity_id (not the PK) on the Entities model.
I want to generate an empty form for all the fields of Breaks. Generating a basic form populates all the empty fields, but for the FK it generates a dropdown list of all objects of the Entities table. This is not helpful so I have excluded this in the ModelForm below and tried to replace with a list of all the entity_ids of the Entities table. This form renders as expected.
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
exclude = ('entity',)
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all().values_list('entity_id', flat=True))
The below FormView is the cbv called by the URL. As the below stands if I populate the form, and for the FK column entity_id choose one of the values, the form will not submit. By that field on the form template the following message appears Select a valid choice. That choice is not one of the available choices.
class ContactFormView(FormView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
My initial thoughts were either that the datatype of this field (string/integer) was wrong or that Django needed the PK of the row in the Entities table (for whatever reason).
So I added a post function to the FormView and could see that the request.body was populating correctly. However I can't work out how to populate this into the ModelForm and save to the database, or overcome the issue mentioned above.
Addendum:
Models added below:
class Entity(models.Model):
pk_securities = models.AutoField(primary_key=True)
entity_id = models.CharField(unique=True)
entity_description = models.CharField(blank=True, null=True)
class Meta:
managed = False
db_table = 'entities'
class Breaks(models.Model):
pk_break = models.AutoField(primary_key=True)
date = models.DateField(blank=True, null=True)
entity = models.ForeignKey(Entity, on_delete= models.CASCADE, to_field='entity_id')
commentary = models.CharField(blank=True, null=True)
active = models.BooleanField()
def get_absolute_url(self):
return reverse(
"item-update", args=[str(self.pk_break)]
)
def __str__(self):
return f"{self.pk_break}"
class Meta:
managed = False
db_table = 'breaks'
SOLUTION
Firstly I got this working by adding the following to the Entity Model class. However I didn't like this as it would have consequences elsewhere.
def __str__(self):
return f"{self.entity_id}"
I found this SO thread on the topic. The accepted answer is fantastic and the comments to it are helpful.
The solution is to subclass ModelChoiceField and override the label_from_instance
class EntityChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return obj.entity_id
I think your problem is two fold, first is not rendering the dropdown correctly and second is form is not saving. For first problem, you do not need to do any changes in ModelChoiceField queryset, instead, add to_field_name:
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all(), to_field_name='entity_id')
Secondly, if you want to save the form, instead of FormView, use CreateView:
class ContactFormView(CreateView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
model = Breaks
In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin. html.
I have a form that allows the user to pick several vans (many-to-many relationship). Each van has a boolean attribute named "available". I want to only show the vans whose "available" attribute is set to "True". How do I do this in the forms.py file?
I know that this could possibly be done in the template, but I did not want to create a new form-template with each individual field written out. I wanted to know if this functionality could be done in the forms.py file or in the class based view. I believe that doing it that way would be a bit cleaner. I've look into the validators but I don't think this is the way to go. Maybe I need to run a query set in the form file that checks the attribute before passing it to the form template?
views.py
def post(self, request):
"""Take in user data, clean it, and then post it to the database."""
form = self.form_class(request.POST) # pass in the user's data to that was submitted in form
if form.is_valid():
trip = form.save(commit=False) # create an object so we can clean the data before saving it
# now get the clean and normalize the data
first_name = form.cleaned_data['first_name']
last_name = form.cleaned_data['last_name']
trip_start = form.cleaned_data['trip_start']
trip_end = form.cleaned_data['trip_end']
van_used = form.cleaned_data['van_used']
trip.save()
forms.py
class TripForm(forms.ModelForm):
"""This class will be used to build trips."""
class Meta:
"""Specifying the database and fields to use."""
model = trips
fields = ['first_name', 'last_name', 'comments','trip_start', 'trip_end', 'van_used']
models.py
class trips(models.Model):
class Meta:
verbose_name_plural = "trips"
van_used = models.ManyToManyField(vans)
class vans(models.Model):
class Meta:
verbose_name_plural = "vans"
vanName = models.CharField(max_length=30, unique=True, blank=False)
available = models.BooleanField(default=True, blank=False)
# set up how the vans will be referenced in the admin page
def __str__(self):
return self.vanName
The final form that is rendered would only show the vans whose "available" attribute is set to True. Thanks in advance.
You have to override queryset for van_used field in form like this.
class TripForm(forms.ModelForm):
"""This class will be used to build trips."""
class Meta:
"""Specifying the database and fields to use."""
model = trips
fields = ['first_name', 'last_name', 'comments','trip_start', 'trip_end', 'van_used']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['van_used'].queryset = vans.objects.filter(available=True)
I have two models with the following relationship defined in models.py:
class InnerModel(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class OuterModel(models.Model):
inner = models.OneToOneField(InnerModel)
def __str__(self):
return "OuterModel"
My forms.py looks like this:
class OuterModelForm(forms.ModelForm)
class Meta:
model = OuterModel
fields = ['inner']
My admin.py form looks like this:
class OuterModelAdmin(admin.ModelAdmin)
form = OuterModelForm
admin.site.register(OuterModel, OuterModelAdmin)
When I display the admin page, I can see the InnerModel instance and the name field is present, but the name field is an empty drop-down menu rather than a blank text field that can be edited.
How can I change the InnerModel name field so that it can be edited by admin?
You need to use inlines (doc):
class InnerModelInline(admin.StackedInline):
model = InnerModel
class OuterModelAdmin(admin.ModelAdmin):
inlines = [InnerModelInline]
admin.site.register(OuterModel, OuterModelAdmin)
Similar question: here
I use Django 1.7.11. I have models:
#I use django-categories app here
class Category(CategoryBase):
pass
class Advertisment(models.Model):
title = models.CharField(max_length=255, blank=True)
category = models.ForeignKey(Category, related_name='category')
all_categories = models.ManyToManyField(Category, blank=True, related_name='all_categories')
I need field "all_categories" contains "category" and all it's parent categories. I tried to use post_save, but it doesn't change any value. It even doesn't change title field. It doesn't work when I create model throught admin interface and works with custom form.
#receiver(post_save, sender=Advertisment, dispatch_uid="update_stock_count")
def update_stock(sender, instance, **kwargs):
categ = instance.category
instance.all_categories.add(categ)
for parent in categ.get_ancestors():
if parent not in instance.all_categories.all():
instance.all_categories.add(parent)
m2m_changed doesn't help too because ManyToManyField is empty and has no changes. How can I add a value from ForeignKey to ManyToMany field? What should I do in order to it works in admin interface.
I've found the solution. In admin class need to add a function save_model like this:
class AdvertismentAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if obj.category:
category_list=[]
category = obj.category
category_list.append(category)
for parent in category.get_ancestors():
if parent not in category_list:
category_list.append(parent)
form.cleaned_data['all_categories'] = category_list
super(AdvertismentAdmin, self).save_model(request, obj, form, change)
I have defined my model with various fields.
Some of these are custom fields, which I am using to validate credit card data, using the fields.py file of my app. Source is here.
class Transaction(models.Model):
card_name = models.CharField()
card_number = CreditCardField(required=True)
security_code = VerificationValueField(required=True)
expiry_date = ExpiryDateField(required=True)
I have defined a ModelForm in my forms.py file.
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = "__all__"
And I've added the form to my admin.py file.
class TransactionAdmin(admin.ModelAdmin):
form = TransactionForm
def get_fieldsets(self, *args, **kwargs):
return (
(None, {
'fields': ('card_name', 'card_number'),
}),
)
admin.site.register(Transaction, TransactionAdmin)
However, the custom fields don't seem to be showing in the administration panel. Having done a ton of research, I found this, which would seem to be the solution, except it doesn't work. I've tried all sorts of over things including adding a fields tuple with the missing fields to get it to work, but no dice. And yes I've done plenty of searching.
The error I get when following the solution in the last link is this:
Unknown field(s) (card_number) specified for Transaction. Check fields/fieldsets/exclude attributes of class TransactionAdmin.
Running Django 1.7.4, Python 3.
Change your model / admin / form like this
class Transaction(models.Model):
card_name = models.CharField()
card_number = models.CharField(max_length=40)
expire_date = models.DateTimeField()
card_code = models.CharField(max_length=10)
class TransactionForm(forms.ModelForm):
card_number = CreditCardField(required=True)
expiry_date = ExpiryDateField(required=True)
card_code = VerificationValueField(required=True)
class Meta:
model = Transaction
fields = "__all__"
class TransactionAdmin(admin.ModelAdmin):
form = TransactionForm
admin.site.register(Transaction, TransactionAdmin)
UPDATE:
CreditCardField is a Form field, not a model field. See its usage in the same link that you have posted.
Those fields will come in the form.