tkhyn / django-gm2m

MIT License
35 stars 23 forks source link

Removed in Django 110 warnings for use of get_field_by_name in monkeypatch #20

Closed tkhyn closed 8 years ago

tkhyn commented 8 years ago

Original report by Ashley Bartlett (Bitbucket: Ash, GitHub: Ash).


I am seeing these errors when I use the related_name parameter on my GM2MField.

#!python

.../site-packages/gm2m/monkeypatch.py:82: RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()'
  gfbn_old(old_names['tgt_fk'])[0],

.../site-packages/gm2m/monkeypatch.py:83: RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()'
  gfbn_new(new_names['tgt_fk'])[0],

.../site-packages/gm2m/monkeypatch.py:86: RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()'
  gfbn_old(old_names['tgt_ct'])[0],

...site-packages/gm2m/monkeypatch.py:87: RemovedInDjango110Warning: 'get_field_by_name is an unofficial API that has been deprecated. You may be able to replace it with 'get_field()'
  gfbn_new(new_names['tgt_ct'])[0],

Look like the fix is pretty simple, replaced with calls to get_field, and removed the list index addressing.

#!python
# gm2m/mokneypatch.py, lines 62 to 91
    if isinstance(old_field, GM2MField) \
    or isinstance(new_field, GM2MField):
        # Repoint the GFK to the other side
        # we need to alter both fields of the GFK
        old_names = old_field.rel.through._meta._field_names
        new_names = new_field.rel.through._meta._field_names
        gfbn_old = old_field.rel.through._meta.get_field
        gfbn_new = new_field.rel.through._meta.get_field

        if old_field.rel.through._meta.db_table == \
        new_field.rel.through._meta.db_table:
            # The field name didn't change, but some options did;
            # we have to propagate this altering.
            self._remake_table(
                old_field.rel.through,
                # We need the field that points to the target model,
                # so we can tell alter_field to change it -
                # this is m2m_reverse_field_name() (as opposed to
                # m2m_field_name, which points to our model)
                alter_fields=[(
                    gfbn_old(old_names['tgt_fk']),
                    gfbn_new(new_names['tgt_fk']),
                ),
                (
                    gfbn_old(old_names['tgt_ct']),
                    gfbn_new(new_names['tgt_ct']),
                )],
                override_uniques=(old_names['src'], old_names['tgt_fk'],
                                  old_names['tgt_ct']),
            )

Too lazy to get setup to do a PR sorry, and haven't done any extensive testing. I am running python3.4 with Django 1.9.

tkhyn commented 8 years ago

Original comment by Ashley Bartlett (Bitbucket: Ash, GitHub: Ash).


I had a look to see when get_field was implemented, it looks like it was around 1.8, or earlier.

tkhyn commented 8 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Hi @abartlett and thanks for the report. This bug has actually been addressed in 7bc1234dbd23, which has not landed in a release yet. That'll come soon.

tkhyn commented 8 years ago

Original comment by Thomas Khyn (Bitbucket: tkhyn, GitHub: tkhyn).


Marking as resolved, feel free to reopen if the issue resurfaces.