自定义django管理页面

时间:2016-10-26 03:25:06

标签: javascript python django postgresql

如果我选择国家/地区,则应在州字段中显示相应的状态。我需要知道如何在模型中实现它。外键自动转换为选择框。如果我选择国家选择框并在下一个选择框(州字段)中显示选项,我们应该如何提取外键值。

#admin.py

from django.contrib import admin

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
#from django.contrib.auth.models import AbstractUser
from models import UserProfile
from models import country

admin.site.register(country)

class UserProfileAdmin(admin.ModelAdmin):
  #model = UserProfile
  can_delete = False
  verbose_name_plural = 'userprofile'

  model = Userprofile
  list_editable = ('country','state')
  list_display = ('state')


#admin.site.unregister(User)
admin.site.register(UserProfile,UserProfileAdmin)


#models.py

from django.db import models
from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible
from django.contrib.auth.models import User
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import AbstractUser
from django.db.migrations.migration import Migration

class country(models.Model):
    #id = models.IntegerField()
    country_id = models.IntegerField()
    country_a2_code = models.CharField(max_length=255)
    country_a3_code = models.CharField(max_length=255)
    country_name = models.CharField(max_length=255)
    #created_date = models.DateTimeField('date published').

    def __unicode__(self):
        return self.country_name


class state(models.Model):
    #id= models.IntegerField()
    state_id = models.IntegerField()
    state_code = models.CharField(max_length=255)
    country_id = models.ForeignKey(country, blank=True, null=True)
    #created_date = models.DateTimeField('date published')

    def __unicode__(self):
        return self.state_code


    #def __str__(self):
        #return self.country_a3_code


class UserProfile(AbstractUser):
    url = models.URLField()
    home_address = models.CharField(max_length=255,default="")
    phone_numer = models.CharField(max_length=255,default="")
    country = models.ForeignKey(country, blank=True, null=True)
    state = models.ForeignKey(state, blank=True, null=True)

如果我选择国家/地区,则应在州字段中显示相应的状态。我需要知道如何在模型中实现它。 外键自动转换为选择框。如果我选​​择国家选择框,我们应该如何提取外键值。

can anyone help this out?

0 个答案:

没有答案