Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access class Meta as a attribute of Django Model Class?

Tags:

In Python I can define:

class Person(object):    name = "Easwar"    age = 35    sex = "male"     class Occupation:       name = "my_job" 

I can then access it

>> p = Person() >> p.Occupation.name >> # prints "my_job" 

However in Django, if I have a model defined with Class Meta inside I cannot do this

>>> m = SomeDjangoModel() >>> m.Meta >>> # prints AttributeError! 

Why is this ? How is Django's inner Meta class different from a regular Python class ?

I did research this and didnt come up with anything similar asked here. Please excuse me if I have missed it.

Thanks in advance for the help.

like image 796
dreaswar Avatar asked Sep 19 '13 14:09

dreaswar


People also ask

How does Django define class meta?

Model Meta is basically used to change the behavior of your model fields like changing order options,verbose_name, and a lot of other options. It's completely optional to add a Meta class to your model. In order to use model meta you have to add class Meta in your model as shown below as follows: class student(models.

What is attribute in model Django?

In Django , a model field pertains to a column in the database. On the other hand, a model attribute pertains to a method or property that is added to a model .

What is __ Str__ in Django model?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model. It is also what lets your admin panel, go from this. Note: how objects are just plainly numbered. to this.

What class does a Django model class inherit?

The basics: Each model is a Python class that subclasses django.db.models.Model . Each attribute of the model represents a database field. With all of this, Django gives you an automatically-generated database-access API; see Making queries.

What is the use of meta class in Django?

The Meta class can be used to define various things about the model such as the permissions, database name, singular and plural names, abstraction, ordering, and etc. Adding Meta classes to Django models is entirely optional. This class also comes with many options you can configure.

Why can’t I create another model field called author in Django?

In Django, this isn’t usually permitted for model fields. If a non-abstract model base class has a field called author, you can’t create another model field or define an attribute called author in any class that inherits from that base class. This restriction doesn’t apply to model fields inherited from an abstract model.

What is a model in Django?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.

Can a child class override a model field in Django?

In normal Python class inheritance, it is permissible for a child class to override any attribute from the parent class. In Django, this isn’t usually permitted for model fields.


1 Answers

The Meta attribute is changed by metaclass.

Try:

SomeDjangoModel._meta 
like image 163
Don Avatar answered Oct 08 '22 17:10

Don