Thursday, September 1, 2016

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

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?



--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/791c4a8a-4cb0-44bc-8a61-5fc1f5afd6d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment