Задать вопрос

Тел: +7 965 3737 888





Link raw_id_fields (both ForeignKeys and ManyToManyFields) to their change pages

UPDATE: Now works in Django 1.4

Based on luc_j:s snippet (http://djangosnippets.org/snippets/2108/) to show values in ManyToManyFields in the admin. This snippets does that, but also links each value to its corresponding admin change page.

To use, just set the raw_id_fields to the value you want, and let your form inherit from ImproveRawIdFieldsForm.

Вопрос полезен? Да0/Нет0

Ответы (6):

Ответ полезен? Да0/Нет0

if you change line 45 to the follow it will also support OneToOneRel's

if type in ('ManyToOneRel', 'OneToOneRel'):

also a good ideal to add the following else after line 48 to avoid losing special handling in super's formfield_for_dbfield for raw_id_fields we don't otherwise handle (assuming there's more than 3 types)

    # we don't have anything special for this type
    return super(ImproveRawIdFieldsForm, self).formfield_for_dbfield(db_field, **kwargs)

Ответ полезен? Да0/Нет0

so... how i use in INLINE form?

Ответ полезен? Да0/Нет0

@AndyHilton: Here's some example usage: https://gist.github.com/EmilStenstrom/4761449

Ответ полезен? Да0/Нет0

This sounds like just what I am looking for but struggling to see where to put the pieces (as with so many django snippets it's hard when it is just raw code without a note to say where it should be placed !) - I tried adding the above to my admin.py but cannot then see how to inherit my form from ImproveRawIdFieldsForm (my form is currently in forms.py) - am sure I am close !! Any helpful pointers ?

Ответ полезен? Да0/Нет0

Updated to work in Django 1.4.

Ответ полезен? Да0/Нет0

Thanks, works perfectly! I can't imagine why this functionality isn't included by default!