English 中文(简体)
django-admin formfield_for_* change commission per/depending on instance
原标题:django-admin formfield_for_* change default value per/depending on instance

I m trying to change the default value of a foreignkey-formfield to set a Value of an other model depending on the logged in user. But I m racking my brain on it...

http://stackoverflow.com/questions/2194728/changeing-foreignkeys-defaults-in-admin-site> 外交部 位于行政站点的主要违约情况是改变空洞的_,但我需要违约值。

#Now I tried the following without errors but it didn t had the desired effect:
class EmployeeAdmin(admin.ModelAdmin):
...
  def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
    formfields= super(EmployeeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
    if request.user.is_superuser:
        return formfields
    if db_field.name == "company":
        #This is the RELEVANT LINE
        kwargs["initial"]  = request.user.default_company
        
    return db_field.formfield(**kwargs)

    
admin.site.register(Employee, EmployeeAdmin)

##################################################################
# REMAINING Setups if someone would like to know it but i think
# irrelevant concerning the problem
##################################################################
from django.contrib.auth.models import User, UserManager
class CompanyUser(User):
    ...
    objects = UserManager()
    company = models.ManyToManyField(Company)
    default_company= models.ForeignKey(Company, related_name= default_company )
    #I registered the CompanyUser instead of the standard User, 
    #   thats all up and working
    ...

class Employee(models.Model):
    company = models.ForeignKey(Company)
    ...

Hint: kwargs[“default”] ......确实存在。

提前感谢尼克

最佳回答

kwargs(最初的)服满了整整整整整整整条,但如果你给它一个物体,它只是给它一个大坝(如我与Kwargs[最初的]=用户.default_company)。 但它与Integer合作。

我解决了这一问题:

def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
    formfields= super(EmployeeAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

    if request.user.is_superuser:
        return formfields

    if db_field.name == "company":
        qs= request.user.company.all()
        #determine the default pos of configured default_company
        for index, item in enumerate(qs):
            if item==request.user.default_company:
                kwargs["initial"]  = index+1
                break
        #restrict shown entries in the ModelChoiceField
        kwargs["queryset"] = qs


    return db_field.formfield(**kwargs)

同情压力,感谢帮助!

问题回答

I think the db_field.formfield method (which is also called by the admin btw) always overwrites your inital value with the default value specified in the model! So you need to specify the right form field yourself:

from django import forms

class EmployeeAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, **kwargs):
        user = kwargs[ request ].user
        if db_field.name == "company":
            kwargs[ initial ] = user.default_company
            qs = Company.objects.all()
            return forms.ModelChoiceField(queryset=qs, **kwargs)
        return super(EmployeeAdmin, self).formfield_for_dbfield(db_field, **kwargs)

您也可以以习俗形式(init)实现这种定制,但问题是,你不能进入request>。 通常以一类形式出现,但你可以在这里看到我的黑板。 Django:如何使目前用户在行政表格中的使用?





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...