class RelatedModel(models.Model):
main = models.ForeignKey(MainModel)
}}}
{{{ManyToOneRel.name}}} should be {{{relatedmodel_set}}}, not
{{{relatedmodel}}}
{{{#!python
>>> from testmodels.models import MainModel
>>> fieldname = MainModel._meta.get_fields()[0].name
>>> print getattr(MainModel, fieldname, None)
None
>>> print getattr(MainModel, fieldname + '_set', None)
<django.db.models.fields.related_descriptors.ReverseManyToOneDescriptor
object at 0x103dc1510>}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27259>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I'm not sure if the proposal is correct. You can use `get_accessor_name()`
to retrieve `relatedmodel_set`. If you provide a patch that shows how to
fix this as your propose and doesn't break any tests, that might confirm
whether or not the idea is correct.
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:1>
* status: new => closed
* resolution: => invalid
Comment:
The relevant attribute is
[https://github.com/django/django/blob/3507d4e773aa9ff2336e7230ba231c4ba6eb568f/django/db/models/fields/reverse_related.py#L71-L73
ForeignObjectRel.name] which is an alias for `related_query_name()`.
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:2>
Comment (by Maxim Syabro):
Tim, what if someone will have this situation:
{{{
class MainModel(models.Model):
relatedmodel = IntegerField()
class RelatedModel(models.Model):
main = models.ForeignKey(MainModel)
}}}
Turns out ReverseManyToOneDescriptor will replace original field. So I
think it's ambiguous a little bit. Don't you think?
{{{
from .models import MainModel
for i, field in enumerate(MainModel._meta.get_fields()):
fieldname = field.name
print i, fieldname
print getattr(MainModel, fieldname, None)
print getattr(MainModel, fieldname + '_set', None)
print
}}}
will give this output
{{{
0 relatedmodel
None
<django.db.models.fields.related_descriptors.ReverseManyToOneDescriptor
object at 0x1076db950>
1 id
None
None
2 relatedmodel
None
<django.db.models.fields.related_descriptors.ReverseManyToOneDescriptor
object at 0x1076db950>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:3>
Old description:
> F.e.
> {{{#!python
> class MainModel(models.Model):
> pass
>
> class RelatedModel(models.Model):
> main = models.ForeignKey(MainModel)
> }}}
>
> {{{ManyToOneRel.name}}} should be {{{relatedmodel_set}}}, not
> {{{relatedmodel}}}
>
> {{{#!python
> >>> from testmodels.models import MainModel
> >>> fieldname = MainModel._meta.get_fields()[0].name
> >>> print getattr(MainModel, fieldname, None)
> None
> >>> print getattr(MainModel, fieldname + '_set', None)
> <django.db.models.fields.related_descriptors.ReverseManyToOneDescriptor
> object at 0x103dc1510>}}}
New description:
--
Comment (by Maxim Syabro):
Okay, I missed this error
{{{
ERRORS:
testmodels.RelatedModel.main: (fields.E303) Reverse query name for
'RelatedModel.main' clashes with field name 'MainModel.relatedmodel'.
HINT: Rename field 'MainModel.relatedmodel', or add/change a
related_name argument to the definition for field 'RelatedModel.main'.
}}}
And I still have this questions:
1. Why does it clash?
2. Why related model have two names: 'relatedmodel' and 'relatedmodel_set'
if we can use only second one?
3. Why name property can't return 'relatedmodel_set' to avoid stuff above?
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:4>
Comment (by Tim Graham):
I can't answer the questions offhand. I'd suggest trying to develop a
patch making the change you suggest and then looking at the test failures
if you want to understand how Django works.
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:5>
Old description:
New description:
Okay, will do. Thanks
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:6>
Old description:
> Okay, will do. Thanks
New description:
F.e.
{{{#!python
class MainModel(models.Model):
pass
class RelatedModel(models.Model):
main = models.ForeignKey(MainModel)
}}}
{{{ManyToOneRel.name}}} should be {{{relatedmodel_set}}}, not
{{{relatedmodel}}}
{{{#!python
>>> from testmodels.models import MainModel
>>> fieldname = MainModel._meta.get_fields()[0].name
>>> print getattr(MainModel, fieldname, None)
None
>>> print getattr(MainModel, fieldname + '_set', None)
object at 0x103dc1510>}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:7>
Comment (by Maxim Syabro):
Okay, will do. Thanks
--
Ticket URL: <https://code.djangoproject.com/ticket/27259#comment:8>