i've changed the app label doing this
class Model(models.Model):
pass
class Meta:
app_label = 'App Name'
db_table = 'app_table'
The table and application开发者_如何学JAVA already existed, the problem is that when i go to the admin interface, only the superusers can view the app, and other users not, i tried to add permissions to the other user but it does not appear in the permissions box.
Thanks in advance!
This is definitely a bug in Django. There's a conflict between the permission app_label and the contenttype app_label, result in the permission never matching in the admin. As a workaround until this is fixed, you can simply explicitly grant permission on the ModelAdmin
:
class MyModelAdmin(admin.ModelAdmin):
...
def has_add_permission(self, request):
return request.user.has_perm('app_label.add_modelclass')
def has_change_permission(self, request, obj=None):
return request.user.has_perm('app_label.change_modelclass')
def has_delete_permission(self, request, obj=None):
return request.user.has_perm('app_label.delete_modelclass')
Where app_label
is the app_label
of the root model and modelclass
is the lowercase name of your proxy model.
I'm not sure you're using app_label as it is intended here. If you're trying to improve the readability of your model's name, use verbose_name in your model's meta class.
app_label doesn't seem to have the best documentation, but from what I can tell it's supposed to be a machine readable name.
app_label
affects database table name and contenttype entries. As if your model wold be moved to other application. Permissions depend on contenttpyes. Syncdb will fix contenttypes, will create new tables, will create new permissions entries. You need to add permissions to existing users/groups on that model which was "moved" to other app.
This may work for you:
Define your Model class as your normally would, ie:
class MyModel(models.Model):
pass
class Meta:
db_table = 'app_table'
Then create a Proxy Model, and change the App Label of that proxy model, for a 2nd model which looks like:
class MyProxyModel(MyModel):
pass
class Meta:
proxy = True
app_label = 'app_name'
Note: Your App label should be all lower case and contain underscores insead of spaces, Django will replace the underscores and capitalize the app label automatically.
Then register for the proxy model whatever ModelAdmin you have created.
admin.site.register(MyProxyModel,MyModelAdmin)
This should get your the MyModelAdmin to show up under a different App label in the Admin Interface. I am not positive this will fix the permissions problem because I don't have an environment to test it in right now, but it will show up under the other other label.
精彩评论