xpetta
xpetta

Reputation: 718

ModelAdmin related issues in Django

Below is my code within the admin.py file.

class JobAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title', 'organization',)}
    save_as = True

admin.site.register(Job, JobAdmin)

Issue #1:

I'm trying to prepopulate my slug using 2 different fields. The title field is getting populated correctly, whereas the organization field is a foreign key field, which is also getting populated but with an integer value. I would like to have this field populated with its original value which is the name field within the Organization model. To achieve this, I did try changing the organization field as follows:

class JobAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug': ('title', 'organization__name',)}

But this is giving me an error.

ERRORS:
<class 'jobs.admin.JobAdmin'>: (admin.E030) The value of 'prepopulated_fields["slug"][0]' refers to 'organization__name', which is not an attribute of 'jobs.Job'.

System check identified 1 issue (0 silenced).

Issue #2:

The save_as = True does not enable the “Save as new” button. I did refer to the Django Admin Document but I'm unable to comprehend what else needs to be done to enable this.

I would be really thankful if anyone could help me in fixing these issues. Thanks for your time and help in advance!

Upvotes: 0

Views: 87

Answers (1)

schillingt
schillingt

Reputation: 13731

Issue 1:

According to the docs, prepopulated fields is done via Javascript. The syntax you're using is a Django ORM syntax. I don't think you can use a related fields property there. You can only use the properties on the given model.

When set, the given fields will use a bit of JavaScript to populate from the fields assigned.

Issue 2:

That's all you should have to do. Is it possible you were looking for the button when you were creating a new Job?

Upvotes: 1

Related Questions