Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
django admin在某些字段中添加具有固定值的数据_Django_Admin - Fatal编程技术网

django admin在某些字段中添加具有固定值的数据

django admin在某些字段中添加具有固定值的数据,django,admin,Django,Admin,我想要的是让用户(管理员)只能将对象中的“facilityid”添加或修改为其admins.Facility中指定的值。 因此,如果某个用户名为UserA,并且具有facility=('FacA','FacB'),当他向数据库添加新对象时,他应该不能添加类似object('Random object','FacC')的内容 此外,他不能将现有对象修改为他不属于的设施 我已使用以下内容过滤对象: class Facilites(models.Model): id = models.Char

我想要的是让用户(管理员)只能将对象中的“facilityid”添加或修改为其admins.Facility中指定的值。 因此,如果某个用户名为UserA,并且具有facility=('FacA','FacB'),当他向数据库添加新对象时,他应该不能添加类似object('Random object','FacC')的内容

此外,他不能将现有对象修改为他不属于的设施

我已使用以下内容过滤对象:

class Facilites(models.Model):
    id = models.CharField(max_length=32, primary_key=True)
    name = models.CharField(max_length=128)

class Objects(models.Model):
    name = models.CharField(max_length=64)
    facilityid = models.ForeignKey(Facilities)

class Admins(models.Model):
    user = models.OneToOneField(User)
    facilities = models.ManyToManyField(Facilities)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Admins.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)
因此,用户只能看到属于其设施的对象。但我不知道如何防止他们在设备之外添加/编辑对象

编辑:

在这里找到了答案:


事实证明,ModelAdmin.formfield\u for\u foreignkey是这种情况下的正确答案:

我可以使用预先制作的设施列表(即,您可以创建一个连接到设施选项的整数字段,供用户选择。)

如果只有管理员能做到这一点,那么权限听起来是相当可行的。您还可以执行表单验证以检查数据库中的错误。根据您拥有的设施数量,您可能需要不同的方法

您也可以对models.CharField使用相同的技术。因此,可能需要为每个设施分配一个3个字母的设施代码,并要求条目匹配3个字母字符串中的一个。您甚至可以在.txt文件中读取该列表。有很多方法可以做到这一点。我将提供一个预先制作的设施列表示例,并从api/模板访问特定用户所属的设施:

def queryset(self, request):
    qs = super(ObjectsAdmin, self).queryset(request)
    if request.user.is_superuser:
        return qs
    return qs.filter(facitityid__id__in = request.user.get_profile().facilities.all())
就查看特定用户所属的设施页面而言,对象之间存在m2m一对一或FK关系。如果是FK或m2m关系,则可以访问该模型类型的其他方法。然而,我不打算在我的示例中使用get_related。一旦进入实例,就可以访问条目集

NYC_FACILITY = 0
LA_FACILITY = 1
ATL_FACILITY = 2

FACILITY_CHOICES = (
    (NYC_FACILITY, 'NYC'),
    (LA_FACILITY, 'LA'),
    (ATL_FACILITY, 'ATL'),

class Facility(models.Model):
    name = models.IntegerField(choices=FACILITY_CHOICES, default="NYC")

    class Meta:
        order_by = ['name']
        verbose_name_plural = "facilities"
        verbose_name = "facility"
    def __unicode__(self):
        return self.name

现在,在您的模板中,您应该能够从用户实例访问协作室集或从协作室实例访问用户集。

协作室列表位于数据库的协作室表中。每个管理员应该能够属于任何和所有的设施,但被允许编辑和添加只属于他的设施的对象。每个对象只属于一个设施。我想在django管理界面中使用它。
# models.py
from django.auth import User
class Person(User):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    facility_loc = models.ForeignKey('Facility') # ForeignKey used assuming only one person can belong to a facility.
    slug = models.SlugField(unique=True)

def get_absolute_url(self):
    return "/%s/%s/" % self.facility_loc % self.slug

# views.py - TemplateView is automatically given a context variable called params which parses data from the URL. So, I'll leave the regex in the URLConf up to you.

class UserFacilityView(TemplateView):
    model = Facility
    template_name = "user_facility.html"