Issue
I have a Customers class that has the attributes (corporate name....) and another that has Customer Data. It is a textfield, but does not appear as a textfield. And I'm calling it in the clients class with models.ForeignKey(OutraClasse, on_delete=models.CASCADE)
. Am I doing it wrong? What is missing?
Edit: These are the models and admins of the app admin:
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)
models
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}"
"""
Solution
Default behavior in Django Admin is that a reference to another model appears as a drop down box. This makes sense because a reference to another model (a foreign key) is just a value that says: "which row in that other table holds the data that is related to this record".
It is not usually an opportunity to directly edit the values in that other table (that other model).
If you are wanting the related model to appear in the django admin as an editable text field, you should take a look at the Django Admin "Inline" classes. You'll need to define an inline for your ClientBasicData and then you'll refer to that from your ClientAdmin.
NOTE: what you are trying to do looks like there might be a more fundamental problem with your model design. You may want to give some thought to other ways of organizing this data (ie, is there really a reason to have the 'basicdata' in another table?).
Answered By - Bill Huneke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.