hello all
i am new in django
i write app about Doctores and Companys
1- the doctore can works with many companies
2- the Company can works with many doctores
3- evry contract between doctore and company has some details
what is the best way to create tables??
models.py
class Doctore(models.Model):
fname = models.CharField(max_length = 50, null=False, blank = False)
.....
.....
def __str__(self):
return self.fname+' '+self.lname
class Company(models.Model):
name = models.CharField(max_length = 200, null=False, blank = False)
.....
....
def __str__(self):
return self.name
class Doctore_Company(models.Model):
doctore = models.ForeignKey("Doctore", on_delete=models.CASCADE)
company = models.ForeignKey("Company", on_delete=models.CASCADE)
contract_begin = models.DateField()
contract_end = models.DateField(null=True, blank = True)
contract_id = models.CharField(max_length=200)
notic = models.TextField()
if this way good
how can insert contracts in doctore page or company page (in admin site)
else
what is the best
Thank
This is good. This will work well for you.
For using this structure in the Admin, see the docs for InlineModelAdmin objects.
KenWhitesell
Thank you for your interest
I tried all the methods according to the explanation, but I did not succeed
If you are requesting assistance with this, please provide more details about what’s not working and post your ModelAdmin class.
from django.contrib import admin
# Register your models here.
from .models import Doctore, Company, Doctore_Company
admin.site.register(Doctore)
admin.site.register(Company)
admin.site.register(Doctore_Company)
You will need to create ModelAdmin classes and use an InlineModelAdmin class for the Doctore_Company
model in each of Doctore
and Company
models as described in the docs.
model.py
class Doctore(models.Model):
doc_id=models.BigAutoField(primary_key=True, unique=True)
fname = models.CharField(max_length = 50, null=False, blank = False)
.....
class Company(models.Model):
comp_id=models.BigAutoField(primary_key=True,unique=True)
name = models.CharField(max_length = 200, null=False, blank = False)
.....
class Doctore_Company(models.Model):
doctore = models.ForeignKey(Doctore, on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
begin = models.DateField()
end = models.DateField(null=True, blank = True)
....
admin.py
from django.contrib import admin
from .models import Doctore, Company, Doctore_Company
class DocInline(admin.TabularInline):
model = Doctore
class CompInline(admin.TabularInline):
model = Company
class ContractAdmin(admin.ModelAdmin):
inlines = [
DocInline,
CompInline,
]
admin.site.register(Doctore)
admin.site.register(Company)
admin.site.register(Doctore_Company, ContractAdmin)
I have tried all methods
And I created new primary keys (doc_id) (comp_id)
but i have 2 error
ERRORS:
<class ‘polls.admin.CompInline’>: (admin.E202) ‘polls.Company’ has no ForeignKey to ‘polls.Doctore_Company’.
<class ‘polls.admin.DocInline’>: (admin.E202) ‘polls.Doctore’ has no ForeignKey to ‘polls.Doctore_Company’.
The Doctore
and Company
should be normal ModelAdmin classes.
The only inline class is Doctore_Company
, and the inline for it is assigned to those other two classes.
Also, when creating ModelAdmin classes, your usage of the admin.site.register
method needs to specify the model and the ModelAdmin class. See the first example box at The Django admin site | Django documentation | Django
So let’s take a step back for a moment. Please show me what a ModelAdmin definition would look like for Doctore
and Company
, _ignoring_the need for Doctore_Company
right now.
yes, now it works propaply
admin.py
from django.contrib import admin
# Register your models here.
from .models import Doctore, Company, Doctore_Company
class DocInline(admin.StackedInline):
model = Doctore_Company
class ContractAdmin(admin.ModelAdmin):
inlines = [
DocInline,
]
admin.site.register(Doctore, ContractAdmin)
admin.site.register(Company, ContractAdmin)
thank you KenWhitesell