Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

django admin connect two model in creation

I have three models

class Voting(Model):
    id = ...


class Candidate(Model):
    voting = ForeignKey(Voting, on_delete=DO_NOTHING)


class Commands(Model):
    voting = ForeignKey(Voting, on_delete=CASCADE, verbose_name="Votación")
    candidate = ForeignKey(Candidate, on_delete=CASCADE)

And I did this en model admin

class CommandInline(admin.TabularInline):
    model = Commands
    extra = 1

@admin.register(VotingCandidate)
class VotingCandidateAdmin(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['voting', "name"]}),
        ('Mensajes', {'fields': ['default_messages', 'votge_ok_mesage', 'vote_error_mesage', ], 'classes': ['collapse']}),
    ]

    inlines = [CommandInline]

But when I press Save I get this error:

Exception Type: RelatedObjectDoesNotExist Exception Value: Commands has no voting.

How I can put in command.voting_id the value of andidate.voting_id in order to can save one Candidate and the cmmands?

question from:https://stackoverflow.com/questions/65907607/django-admin-connect-two-model-in-creation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...