Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#7446 closed (invalid)

[newforms-admin] inline editing with more than one foreign key to same model

Reported by: duke@… Owned by: nobody
Component: contrib.admin Version: newforms-admin
Severity: Keywords: m2m fk inline foreign key
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Given the following: 'ModelB' is edited inline with 'ModelA', and ModelB has more than one foreign key to ModelA.

If any of the foreign key fields in ModelB are left blank, then they should default to the value currently being added in ModelA.

In the case where there are no ModelA objects all foreign key fields in ModelB will be set to the object currently being added in ModelA - a probable combination for this type of model anyway. mostly solved my problem for an inline interface for this type of model, however if I leave the second foreign key reference blank I get an error indicating that a selection is required. I think it'd be better to just have it use the value being entered in the field that was specified as 'fk_name'.

Change History (3)

comment:1 Changed 11 years ago by Brian Rosner

Resolution: invalid
Status: newclosed

I don't think you understanding the documentation. Given your ModelA and ModelB example there is *always* only one foreign key that will be assigned to the new ModelB instance. The other foreign key is meant to be selected by the user. It would not make any sense always create an object with *both* foreign keys to the same object.

comment:2 Changed 11 years ago by duke@…

That depends on the model; here's an example (I wouldn't have though of this if I didn't have a use case!):

class Language(models.Model):
    id = models.CharField(max_length=5, primary_key=True)

class Language_locale(models.Model):
    language = models.ForeignKey(Language, related_name='language_id')
    displayLanguage = models.ForeignKey(Language, related_name='display_name')
    displayName = models.CharField(max_length=16)

A Language id would be something like 'en', 'fr', or 'en_ca', 'fr_ca', etc. A Language_locale defines the human-readable form of a language name. For example, the entry ('en', 'en', 'English') indicates that the human-readable name of the Language 'en' in the locale 'en' is 'English'. The entry ('en', 'fr', 'anglais') indicates that the human-readable name of the Language 'en' in the locale 'fr' is 'anglais'.

I guess it doesn't matter since I can just save a new Language entry then select it in the secondary foreign key field in Language_locale when I want to create an entry where both foreign keys point to the same Language. I'll have to click 'save' twice though..

comment:3 Changed 11 years ago by Brian Rosner

Your use case is quite specific and changing the default behavior for this would just not be the right thing. However, you might want to read into model formsets since you can override save behaviors of the inline and do just about whatever you like.

Note: See TracTickets for help on using tickets.
Back to Top