devdc
devdc

Reputation: 161

Django Admin add form and manipulate model data

I have a ModelAdmin which I wish to add a custom form into:

class PlaceAdminForm(forms.ModelForm):
    lat = forms.DecimalField()
    lon = forms.DecimalField()

    class Meta:
        model = Place

class PlaceAdmin(admin.ModelAdmin):
    search_fields = ["name"]
    form = PlaceAdminForm

    def save_model(self, request, obj, form, change):
        obj.gps_location = Point(self.form.lat, self.form.lon)
        obj.save(request)
admin.site.register(Place, PlaceAdmin)

When I run the server and try to add a Place I get the error: type object 'PlaceAdminForm' has no attribute 'lat'

What am I missing here?

Upvotes: 0

Views: 235

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

As with any Django form, you get the validated data from the cleaned_data dictionary.

Point(form.cleaned_data['lat'], form.cleaned_data['lon'])

Upvotes: 1

Related Questions