0

Can we say that Django models are considered dataclasses? I don't see @dataclass annotation on them or on their base class model.Models. However, we do treat them like dataclasses because they don't have constructors and we can create new objects by naming their arguments, for example MyDjangoModel(arg1= ..., arg2=...). On the other hand, Django models also don't have init methods (constructors) or inherit from NamedTuple class.

What happens under the hood that I create new Django model objects?

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • Not sure what you want to undertand ? there is no dataclasses annotation in Django project. Because there is no need of this. If you create an instance of ORM like `Object()`, Django do nothing, you have to call `object.save()` for triggering sql query if it is the question. Or you can use manager `Object.objects.create()` which make directly a sql query for creating object in database. – Lucas Grugru Nov 08 '22 at 16:35
  • dataclass is very recent annotation in python. Django has build ORM without this tools – Lucas Grugru Nov 08 '22 at 16:37
  • Django model classes don't have an `__init__`? You haven't looked deep enough I'd say (Since obviously your models _inherit_ from the [django.db.models.Model](https://github.com/django/django/blob/e0fb2a25b973a5e539d908f7f31112fb10622bd4/django/db/models/base.py#L459) class. Even if this weren't to be there there's always `__init__` from `object` plus these really aren't constructors so to speak – Abdul Aziz Barkat Nov 08 '22 at 17:51
  • Does this answer your question? [How do Django models work?](https://stackoverflow.com/questions/12006267/how-do-django-models-work) – Abdul Aziz Barkat Nov 08 '22 at 17:55
  • Does my answer answer you question or can I elaborate somehow further? Happy to do so for it to be accepted :) – Swift Nov 17 '22 at 22:30

2 Answers2

1

A lot of the magic that happens with models, if not nearly all of it, is from its base meta class.

This can be found in django.db.models.ModelBase specifically in the __new__ function.

Regardless of an __init__ method being defined or not (which actually, it is as per Abdul's comment), doesn't mean it can or should be considered a dataclass.

As described very eloquently in this SO post by someone else;

What are data classes and how are they different from common classes?

Despite django models quite clearly and apparently seeming to have some kind of data stored in them, the models are more like an easy to use (and reuse) set of functions which leverage a database backend, which is where the real state of an object is stored, the model just gives access to it.

It's also worth noting that models don't store data, but simply retrieves it.

Take for example this simple model:

class Person(models.Model):
    name = models.CharField()

And then we did something like this in a shell:

person = Person.objects.get(...)
print(person.name)

When we access the attribute, django is actually asking the database for the information and this generates a query to get the value.

The value isn't ACTUALLY stored on the model object itself.

With that in mind, inherently, django models ARE NOT dataclasses. They are plain old regular classes.

Swift
  • 1,663
  • 1
  • 10
  • 21
  • There actually is an `__init__` method, see [here](https://github.com/django/django/blob/e0fb2a25b973a5e539d908f7f31112fb10622bd4/django/db/models/base.py#L460) – Abdul Aziz Barkat Nov 08 '22 at 17:52
  • I think I worded that poorly, I updated it to be more clear, but yes, tha k you for your comment. – Swift Nov 08 '22 at 17:54
1

Django does not work with data classes. You can define a custom model field. But likely this will take some development work.