archang31
archang31

Reputation: 31

Purpose of Django FieldType Default String Parameter

In the Django 2.0 Mozilla tutorial, they sometimes use a initial string argument when defining various fieldTypes as part of there new models. For example, they do this with the author variable (ForeignKey FieldType) and isbn (CharField) in the below snippet.

class Book(models.Model):
    """
    Model representing a book (but not a specific copy of a book).
    """
    title = models.CharField(max_length=200)
    author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)
    # Foreign Key used because book can only have one author, but authors can have multiple books
    # Author as a string rather than object because it hasn't been declared yet in the file.
    summary = models.TextField(max_length=1000, help_text='Enter a brief description of the book')
    isbn = models.CharField('ISBN',max_length=13, help_text='13 Character <a href="https://www.isbn-international.org/content/what-isbn">ISBN number</a>')

What is the purpose of this string? I looked through the Django Model Documentation and could not find an initial string parameter as an option. I assume it is the value used for the column in the database, but that is specified with the optional parameter db_column. Any insight is appreciated. Thanks.

Upvotes: 1

Views: 177

Answers (2)

Harun ERGUL
Harun ERGUL

Reputation: 5942

There are two ways to specify ForeingKey One of them is using direct reference to model. In this case there should be Author model in you app.

author = models.ForeignKey(Author, on_delete=models.SET_NULL, null=True)

The other one is using string name. In this case you can later add your Author model in django. Django will not give error. Sometimes you want to create another model later but you also want to reference it now. If this is the case then use String as a parameter version.

 author = models.ForeignKey('Author', on_delete=models.SET_NULL, null=True)

Now you can create your Book model without creating Author model.

For ISBN field this parameter is verbose_name. Name of the column in the table. If you will not specify django automatically create from field name. In this case, programmer wants isbn column in the database as capital case ISBN. You can find more details about verbose_name in official documentation.

Upvotes: 1

JPG
JPG

Reputation: 88569

When we look into the source code of both classes (more specifically, their __init__() method), (ForeignKey and CharField) we could find something like below

ForeignKey

class ForeignKey(ForeignObject):
    ....
    ....

    def __init__(self, to, on_delete=None, related_name=None, related_query_name=None,
                 limit_choices_to=None, parent_link=False, to_field=None,
                 db_constraint=True, **kwargs):
    # code
    # code



CharField
This class is inherited from Field` class, so

class Field(RegisterLookupMixin):
    ...
    ...
    ... code

    def __init__(self, verbose_name=None, name=None, primary_key=False,
                 max_length=None, unique=False, blank=False, null=False,
                 db_index=False, rel=None, default=NOT_PROVIDED, editable=True,
                 serialize=True, unique_for_date=None, unique_for_month=None,
                 unique_for_year=None, choices=None, help_text='', db_column=None,
                 db_tablespace=None, auto_created=False, validators=(),
                 error_messages=None):
        # code
        # code


Conclusion
The first parameter to the classes are different in both classes.CharField takes verbose_name as first argument whereas ForeignKey takes to which the raled models name as the first parameter

Resources

  1. CharField source code
  2. Field source code
  3. ForeignKey source code

    Hope this clear your doubt :)

Upvotes: 1

Related Questions