Setting model level permissions in code for Django admin panel - python

Is there a way to implement the permissions for models through the code? I have a large set of models and I want some of the models to be just viewable by the admin and not the ability to add them.
Please suggest.

You can set permissions through the admin site itself. For instructions, see the "Users, Groups and Permissions" section in the django book chapter:
The Django Administration Site

Found the solution here: http://code.djangoproject.com/wiki/RowLevelPermissions
works the way I needed.

Related

Django create a custom form permission

I'm developing a management software. And I need create a module for manage the permissions and groups using the auth of django. I dont want use the admin django because this just allow log in for super users.
I want override the admin route and create a form with the same features from the admin site. If is possible, I want use the widget for the assignment of permission and group.
I need all this built into an app because I need this to work for this and other projects.
I have already written a custom form to add, edit and view users extending the class UserCreationForm, I need something similar to that.
I hope you can help me...
First things first: don't do this!
Creating your own Django admin site is a load of work, and likely to be insecure etc. Your'e opening a giant can of worms here.
If you need members of your app to edit permissions, they do not have to be superusers! Users with is_staff = True can all access the admin site. Once you've set this for the users you want, go ahead and configure the exact permissions for this type of user.
Start with the official docs on user permissions.

Let site administrator manage default values in Django

I need to let the site administrator set few defaults, without embedding them to settings.py.
For example the admin sets some value, that will be default in some models for 3 months, after that admin changes it to another.
How to implement it nicely in Django, those defaults being able to be used in other models ?
I think you are looking for django-livesettings, which is used in satchmo project.

Using Django Admin History with a custom Login_backend and user model

I have a django application using a custom (ldap) login backend and my own extended user model that is used by other applications.
I'm using the django admin extensively but I've only just noticed that the history link leads to a yellow page. I realised that this was due to the fact that I was not using django.contrib.auth and so the auth_user table did not exist.
I uncommented that in my settings.py and the yellow screen is gone.
The history functionality is not working though and I'm guessing that's because the changes are stored against request.user but the lookup is searching for the user in auth_user.
Does anyone know how to make django admin use my user model table?
If not, does anyone have any idea how I could remove the history link from the admin object view?
Note: I'm looking for the solution with the least editing of the django source code as I will have to justify every line to my superiors.
Any help/suggestions/criticism would be very welcome
Thanks,
Hayden
You can define what is shown in the history of each model by customizing the history view, or you can simply remove the link from the admin site by overriding the admin templates.
Either of these are recommended as they are upgrade friendly and don't involve any changes to the django codebase.
To use your custom model for the built-in history feature (and other related options) see the responses to this question.

Django Pinax , extending bundled applications

I want to use Pinax for a small project , but I am confused because I don't if can extend/change the behavior and functional of the provided applications .
Is there any documentation for extending the behavior of the bundled applications ?
example: in registration application ,I want to add custom fields but I am not able to find proper documentation on how to achieve it..( mainly for those which need db changes )
Thanks !
Yes, you can extend the behaviour of the built-in applications. If you are using the pinax basic setup with user accounts and profiles, you will have to add the extra fields you want in apps/profiles/models.py. For a list of field types, see here: https://docs.djangoproject.com/en/1.3/ref/models/fields/
This will create the necessary db fields for you when you run manage.py syncdb. If you have already sync'd the db, however, you will have to manually add the db columns. If you don't have any data you care about in that table, you can always just drop the table and it will recreate it. Django doesn't modify db tables once they are created, even if you change the model.
You will also have to modify the signup form to include these new fields and point your urls.py to the new signup form you created. Copy the form from the site-packages/pinax directory to your project. Don't modify them directly.
If you haven't already, you should check out the Django tutorial here: https://docs.djangoproject.com/en/1.3/intro/tutorial01/
This will give you a good idea of how Django apps are put together and how the different pieces interact, so you can do a better job customizing Pinax to your liking. Make sure you know what models.py, urls.py, views.py, and the templates are doing.

Where to you monkey patch the Django user model?

I want to monkey patch the user model from Django.
My code:
from django.db import models
from django.contrib.auth.models import User
User.add_to_class('secret_question', models.CharField(max_length="100"))
User.add_to_class('answer', models.CharField(max_length="100"))
User.add_to_class('DOB', models.DateField())
Where do I place this code so that python manage.py syncdb will create the correct table?
I tried the main directory models.py, I tried an app's directory's models.py (these two didn't produce the correct table), and I tried placing it in the settings.py of the project (error, couldn't run).
Please take a look at Storing additional information about users section of the authentication documentation. It suggests a cleaner way to add additional information to a User object.
If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.
If you really want to monkey patch user model, there already exists an app for this.
Django-primate
A modular django user.
This Django application monkey patches
django in order to have a custom User
model that plugs into the
django.contrib.auth application.

Categories