Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verbose_field_name broken for reverse rels w/o related_name #1089

Open
rpkilby opened this issue Jun 18, 2019 · 0 comments
Open

verbose_field_name broken for reverse rels w/o related_name #1089

rpkilby opened this issue Jun 18, 2019 · 0 comments

Comments

@rpkilby
Copy link
Collaborator

rpkilby commented Jun 18, 2019

I'm surprised I haven't noticed this yet, but verbose_field_name is broken for reverse relationships that don't set related_name. This is an extension of #716. Example:

class Article(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    ...

verbose_field_name(User, 'article') should return article, but chokes.

I need to look into this further, but I'm thinking the following:

if isinstance(part, ForeignObjectRel):
if part.related_name:
names.append(part.related_name.replace('_', ' '))
else:
return '[invalid name]'
else:
names.append(force_text(part.verbose_name))

should be:

    if isinstance(part, ForeignObjectRel): 
        meta = part.related_model._meta
        names.append(force_text(meta.verbose_name))
    else:
        names.append(force_text(part.verbose_name))

The downside would be that you'd lose the context of the reverse related name. Need to think about it, but not sure if it'd be more or less correct. Alternatively, we could do something like:

    if isinstance(part, ForeignObjectRel): 
        if part.related_name: 
            # not sure if pretty_name or just .replace(...)
            names.append(pretty_name(meta.verbose_name))
        else:
            meta = part.related_model._meta
            names.append(force_text(meta.verbose_name))
    else:
        names.append(force_text(part.verbose_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant