Kartheek Ravula
Kartheek Ravula

Reputation: 23

Django Inheritance project

Sir/Madam, I'm trying here to inherit a project in Django. I wrote here 1 single CommonData and 3 children classes. Trying to inherit 2 common data fields 'name' and 'location'. I code in the admin.py file also. I think everything I wrote is correct. But, I got the following error while making migrations. I don't know where I done a mistake? please give me a hint to fix it. Thank you.

 ERRORS:
    <class 'abapp.admin.CustomerAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'name', which is not a callable, an attribute of 'CustomerAdmin', or an attribute or method on 'abapp.Customer'.
    <class 'abapp.admin.CustomerAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'location', which is not a callable, an attribute of 'CustomerAdmin', or an attribute or method on 'abapp.Customer'.
    <class 'abapp.admin.EmployeeAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'name', which is not a callable, an attribute of 'EmployeeAdmin', or an attribute or method on 'abapp.Employee'.
    <class 'abapp.admin.EmployeeAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'location', which is not a callable, an attribute of 'EmployeeAdmin', or an attribute or method on 'abapp.Employee'.
    <class 'abapp.admin.StudentAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'name', which is not a callable, an attribute of 'StudentAdmin', or an attribute or method on 'abapp.Student'.
    <class 'abapp.admin.StudentAdmin'>: (admin.E108) The value of 'list_display[1]' refers to 'location', which is not a callable, an attribute of 'StudentAdmin', or an attribute or method on 'abapp.Student'.

models.py file

from django.db import models

# Create your models here.
class CommonData(models.Model):
    name = models.CharField(max_length=20)
    location = models.CharField(max_length=20)
    class Meta:
        abstract = True

class Student(models.Model):
    # name = models.CharField(max_length=20)
    # location = models.CharField(max_length=20)
    marks = models.IntegerField()
    clg_name = models.CharField(max_length=20)

class Employee(models.Model):
    # name = models.CharField(max_length=20)
    # location = models.CharField(max_length=20)
    salary = models.IntegerField()
    company = models.CharField(max_length=50)

class Customer(models.Model):
    # name = models.CharField(max_length=20)
    # location = models.CharField(max_length=20)
    sales = models.IntegerField()
    email = models.EmailField(max_length=50)

Upvotes: 1

Views: 104

Answers (1)

Anvesh
Anvesh

Reputation: 625

You should be fixing inheritance in the active models from CommonData abstract model. Checkout the corrections made in defining Student, Employee, Customer model classes. After changing, it should be working most likely.

from django.db import models

# Create your models here.
class CommonData(models.Model):
    name = models.CharField(max_length=20)
    location = models.CharField(max_length=20)
    class Meta:
        abstract = True

class Student(CommonData):
    marks = models.IntegerField()
    clg_name = models.CharField(max_length=20)

class Employee(CommonData):
    salary = models.IntegerField()
    company = models.CharField(max_length=50)

class Customer(CommonData):
    sales = models.IntegerField()
    email = models.EmailField(max_length=50)

Upvotes: 1

Related Questions