views:

82

answers:

2

I have a model that represents a position at a company:

class Position(models.Model):
    preferred_q = ForeignKey("Qualifications", blank=True, null=True, related_name="pref")
    base_q = ForeignKey("Qualifications", blank=True, null=True, related_name="base")

    #[...]

It has two "inner objects", which represent minimum qualifications, and "preferred" qualifications for the position.

I have a generic view set up to edit/view a Position instance. Within that page, I have a link that goes to another page where the user can edit each type of qualification. The problem is that I can't just pass the primary key of the qualification, because that object may be empty (blank and null being True, which is by design). Instead I'd like to just pass the position primary key and the keyword "preferred_qualification" or "base_qualification" in the URL like so:

(r'^edit/preferred_qualifications/(?P<parent_id>\d{1,4})/$', some_view),

(r'^edit/base_qualifications/(?P<parent_id>\d{1,4})/$', some_view),

Is there any way to do this using generic views, or will I have to make my own view? This is simple as cake using regular views, but I'm trying to migrate everything I can over to generic views for the sake of simplicity.

A: 

As explained in the documentation for the update_object generic view, if you have ParentModel as value for the 'model' key in the options_dict in your URL definition, you should be all set.

Steef
In this case you'd be editing/updating ParentModel. My impression (although admittedly it isn't entirely clear from the question) is that OP wants to edit an instance of InnerModel instead.
Carl Meyer
A: 

If you want the edit form to be for one of the related instances of InnerModel, but you want to pass in the PK for ParentModel in the URL (as best I can tell this is what you're asking, though it isn't very clear), you will have to use a wrapper view. Otherwise how is Django's generic view supposed to magically know which relateed object you want to edit?

Depending how consistent the related object attributes are for the "many models" you want to edit this way, there's a good chance you could make this work with just one wrapper view rather than many. Hard to say without seeing more of the code.

Carl Meyer