简体   繁体   中英

django model relationship to two other models

I have two models in a django app, Doctor and the Django Built in User model. Each one can have many registered phone numbers and addresses. i decided to create an addressbook app to store addresses and phone numbers.

models.py in addressbook app

from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.db import models
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import permalink
from django.contrib.auth.models import User
from django.conf import settings
from doctors.models import Doctor
from django.core.validators import RegexValidator


class AddressPhoneType(models.Model):
    type_name = models.CharField(_('Type'), max_length=50)

    class Meta:
        verbose_name = _('Address / Phone Type')
        verbose_name_plural = _('Address / Phone Type')

    def __unicode__(self):
        return u'%s' % self.type_name


class City(models.Model):
    city = models.CharField(_('City'), max_length=50)

    class Meta:
        verbose_name = _('City')
        verbose_name_plural = _('Cities')

    def __unicode__(self):
        return u'%s' % self.city


class Address(models.Model):
    """Address model."""
    body = models.TextField(_('Address'))
    city = models.ForeignKey(City, null=True)
    lng = models.DecimalField(max_digits=9, decimal_places=6)
    lat = models.DecimalField(max_digits=9, decimal_places=6)

    #content_type = models.ForeignKey(ContentType, related_name="content_type_address")
    #object_id = models.PositiveIntegerField()
    #content_object = GenericForeignKey()

    doctor = models.ForeignKey(Doctor, null=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
    address_type = models.ForeignKey(AddressPhoneType)

    class Meta:
        verbose_name = _('Address')
        verbose_name_plural = _('Addresses')

    def __unicode__(self):
        return u'%s' % self.body


class PhoneNumber(models.Model):
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="""Phone number must be entered in the format: 
        '+999999999' or '999999999'. Up to 15 digits allowed.""")
    phone_number = models.CharField(validators=[phone_regex], blank=True, max_length=15)
    description = models.CharField(_('Description'), max_length=100)
    phone_type = models.ForeignKey(AddressPhoneType)
    doctor = models.ForeignKey(Doctor, null=True)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)

    class Meta:
        verbose_name = _('Phone Number')
        verbose_name_plural = _('Phone Number')

    def __unicode__(self):
        return u'%s - %s' % (self.phone_number, self.description)

if you look under class Address, there is two foreign keys, one to a user and the other one to doctor, so each address entry can be associated with either one (same mechanics apply for phones). the problem is that when i go to the admin page and i add a doctor (I have the address model set to be shown inine in admin.py in doctors app), i see a user drop down to select a user for that added address which does not seem correct. 在此处输入图片说明

I tried to use ContentType and GenericForeignKeys but i dont think they are the solution here....Any suggestions ?

I guess you have created an AddressInline inside the DoctorAdmin , if thats the case you can just exclude the user from the inline .

from django.contrib import admin

class AddressInline(admin.TabularInline):
    model = Address
    exclude = ('user',)

class DoctorAdmin(admin.ModelAdmin):
    inlines = [
        AddressInline,
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM