Home  >  Article  >  Backend Development  >  How to make one class inherit elements of another class in Django?

How to make one class inherit elements of another class in Django?

WBOY
WBOYforward
2024-02-09 22:09:03385browse

Django 中如何让一个类继承另一个类的元素?

Question content

I have a customer class with attributes (company name...) and another with customer data. It is a text field but is not displayed as a text field. I call it using models.foreignkey(outraclasse, on_delete=models.cascade) in the client class. Did i do something wrong? What's missing?

edit: These are the models and managers of the application administrator:

from django.contrib import admin
from tenants.models import client

# register your models here.

class clientadmin(admin.modeladmin):
    list_display = [
        'company_name',
        'company_register_name',
        'company_id']

    search_fields = [
        'company_name',
        'company_register_name',
        'company_id']

    list_per_page = 10

admin.site.register(client,clientadmin)

model

from django.db import models                                                                                                                                             
from datetime import date                                                                                                                                                
                                                                                                                                                                         
                                                                                                                                                                         
                                                                                                                                                                         
class ClientBasicData(models.Model):                                                                                                                                     
    # client_id = models.ForeignKey(Client, on_delete=models.CASCADE)                                                                                                    
    name_admin_ti = models.CharField(max_length=30,                                                                                                                      
                                     verbose_name = u'Nome do responsavel TI',                                                                                           
                                     unique=False)  
    def __str__(self):
        return f"{self.name_admin_ti}"


class Client(models.Model):
  company_data = models.ForeignKey('ClientBasicData', 
                                     on_delete=models.CASCADE,
                                     verbose_name= u'Dados do 
                                     cliente',
                                     unique=False)
  company_name = models.CharField(max_length=30,
          verbose_name = u'Razao social',
          unique=False)
  company_register_name = models.CharField(max_length=30,
          verbose_name = u'Nome da Empresa',
          unique=True)
  company_id = models.CharField(
          max_length=30,
          verbose_name = u'CNPJ da empresa',
          unique=True)
  date_start_company = models.DateField(
          verbose_name = u'Data de ingresso de cliente',
          unique=False,
          default=date.today,
          null=True)

  def __str__(self):
      return f"{self.company_name} - {self.company_register_name} - {self.company_id}"

"""
class ClienteBasicData(models.Model):
    client_id = models.ForeignKey(Client, on_delete=models.CASCADE)
    name_admin_ti = models.CharField(max_length=30,
                                     verbose_name = u'Nome do responsabel TI',
                                     unique=False)

    def __str__(self):
        return f"{self.name_admin_ti}"
"""

Correct answer


The default behavior in Django Admin is for a reference to another model to appear as a dropdown box. This makes sense because a reference to another model (foreign key) is just a value that says: "which row in another table holds the data related to this record".

There is usually no opportunity to directly edit values ​​in other tables (other models).

If you want the related model to appear in Django admin as an editable text field, you should take a look at the Django admin "inline" class. You need to define the ClientBasicData inline and then reference that data from the ClientAdmin.

NOTE: What you are trying to do looks like there may be a more fundamental problem with your model design. You might want to think about other ways of organizing this data (i.e., is there really a reason to put the "basic data" in another table?).

The above is the detailed content of How to make one class inherit elements of another class in Django?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete