I want to get all fields of a model including reverse fields using _meta.get_fields() but i noticed that some Fields returned are missing the ‘_set’ for reverse relation
Here is an example of 2 models linked with ForeignKey
class Version(models.Model):
revision = models.ForeignKey(
Revision,
on_delete=models.CASCADE,
help_text="The revision that contains this version.",
# related_name="version"
)
class Revision(models.Model):
date_created = models.DateTimeField(
db_index=True,
verbose_name=_("date created"),
help_text="The date and time this revision was created.",
)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True,
null=True,
on_delete=models.SET_NULL,
verbose_name=_("user"),
help_text="The user who created this revision.",
)
comment = models.TextField(
blank=True,
verbose_name=_("comment"),
help_text="A text comment on this revision.",
)
So when i use
Revision._meta.get_fields()
output:
(<ManyToOneRel: reversion.version>,
<django.db.models.fields.AutoField: id>,
<django.db.models.fields.DateTimeField: date_created>,
<django.db.models.fields.related.ForeignKey: user>,
<django.db.models.fields.TextField: comment>)
Revision._meta.get_fields()[0].name
Out: ‘version’
i think it should be ‘version_set’ because there is no related_name in the ForeignKey
when i do Revision.version i get AttributeError
What is wrong with Revision._meta.get_fields()[0].name? am i missing something