Disadvantages of using model name rather than model class for FK field?

45 views
Skip to first unread message

James Beith

unread,
Sep 1, 2016, 7:18:06 AM9/1/16
to Django users
A ForeignKey field is often defined using the class to which the model is related, for example.

from django.db import models
from authors.models import Author


class Post(models.Model):
    author = models.ForeignKey(Author)


Alternatively the model name can be used, rather than the model class, for example.


from django.db import models


class Post(models.Model):
    author = models.ForeignKey('authors.Author')


By using a model name there are fewer imports at the top of the module and fewer occurrences of circular import dependencies between Django applications. What are the disadvantages to using a model name and why not always use them?



Javier Guerra Giraldez

unread,
Sep 1, 2016, 8:39:31 AM9/1/16
to django...@googlegroups.com
On 1 September 2016 at 11:55, James Beith <ja...@beith.co.uk> wrote:
> By using a model name there are fewer imports at the top of the module and
> fewer occurrences of circular import dependencies between Django
> applications. What are the disadvantages to using a model name and why not
> always use them?


totally a wrong reason, but i like using classes instead of names
because that way it feels more "programming" and less "configuration".

a _possible_ (but not really convincing) reason would be that it makes
it easier for tools (syntax highlight, autocompleting editors, static
analyzers...) to detect typos.

--
Javier

ludovic coues

unread,
Sep 1, 2016, 8:48:13 AM9/1/16
to django...@googlegroups.com
Class might be easier to use for beginner and is explicit. There is an
import, you know what you are dealing with and you get clear error
when you do things wrong.

If you have circular depencencies, using model name can be the only solution.
If you have model A with a foreign key on model B and model B with a
foreign key on model A, both in the same file, using class is a no go.
You will try to reference a model that is not yet declared, causing
error.
That's the primary use case for model name.
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFkDaoQhr%2BjVo5PRS%2B5x-0j88r-4VEafVk0Pz7Cg-foUsWX7Qg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

James Beith

unread,
Sep 1, 2016, 9:14:01 AM9/1/16
to Django users
Yeah you're right about the development tools and being quicker for refactoring and alike. I believe typos will be caught when running `makemigrations` at least as it will complain about not being about to find the model.
Reply all
Reply to author
Forward
0 new messages