I am writing a Django application and I need to create a personalized admin model, in order to show only determined items in the administration interface when you create a new object.
I wrote this code for admin.py:
from django import forms
from myapp.models import *
from django.contrib import admin
class SimAdmin(admin.ModelAdmin):
phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))
fields = ('phone', 'num_phone', 'pin', 'puk')
admin.site.register(Item)
admin.site.register(Sim, SimAdmin)
...
The problem is that when I log in to the admin site, my SimAdmin does not appear. Django does not give me any error in this code, but if I write only
...
admin.site.register(SimAdmin)
...
it gives me a
TypeError: 'MediaDefiningClass' object
is not iterable
in this line.
I searched in the web and the documentation for this error, but I didn't found anything relevant about my concrete problem. I think the solution will be very simple, but I can't see it. Can anybody help me?
Thanks!
this is incorrect:
class SimAdmin(admin.ModelAdmin):
phone = forms.ModelChoiceField(queryset=Item.objects.filter(name='phone'))
you're trying to attach forms.Field to admin.ModelAdmin. It doesn't work this way.
Take a look at how can it be implemented: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany
Related
I have done a django web app in which users can input some data. I have created a super user and as the admin of the app I can see the data input by users which is fine for name and not sensitive data but I do not want to be able to see their sensitive data such as health data.
I have used the encrypt module from django_cryptography.fields as follow:
health_data = encrypt(models.IntegerField(default=140))
I figured out that if I am looking at the database from external script or simple DBbrowser, it works well as I cannot see the sensitive data. But I understood that this is seamless in the django admin: in django admin page it is decrypting before it is rendered.
So I am ok with the encrypt data which is additional safety but this was not my first goal. I want to have the super user but I want that I am not able to see their sensitive data. Do you have any ideas ? I appreciate your time reading.
As suggested here I changed my admin code from this:
from django.contrib import admin
from .models import MyModel
# Register your models here.
admin.site.register(MyModel)
to this:
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
# avoid admin can see the sensitive data in admin page
fields = ("non_sensitive_field1", "non_sensitive_field2",...,)
By this way I customize the rendered fields in admin page. I cannot see anymore the sensitive data from users in the admin page which is the behavior I wanted.
It is possible to build a project in Django without models ? I have a views, templates(html), css and urls. That site is looking very good in a browser. It is a hairdressing salon website.
Greetings
It is completely possible to create a Django project without any models. You only really need models if your website contains objects, like posts or users.
For one example, I created a recreation of Wikipedia using Django using exactly 0 models; since each entry was just a Markdown file, there was no need for any models.
Sure thing! Just keep in mind that Django's built in admin suite needs a superuser to access, so if you want to manage your website from that side of things, you will need to put in at least a User model, but thankfully that is pretty easy.
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
pass
If this is your models.py file, you can just run python manage.py createsuperuser
I have been learning python for some months, and started tinkering with Django. Before posting this, I read up on the auto-generated 'admin.py'-code on github, as well as googled the matter. It appears my question is a little specific, and I was quite frankly very confused from that specific reading. Thus, I hope asking this adds value to this wonderful community. Question:
When connecting a model to the admin page, in admin.py, you first import admin:
from django.contrib import admin
After this, you import your model. Then, you supposedly connect your model through:
admin.site.register(MODEL)
What I do not understand is what 'site.register' is. The fact that the line starts off with 'admin.' makes perfect sense, as you are specifying from where the following import (ex. 'admin.function' or 'admin.class') comes from. Had it only been 'admin.somefunctionfromadmin' I would have totally understood this. Now, instead, I am confused as to what 'sites.register' is.
Is 'sites' a module, a file, and 'register' a function from within that module? If so, what does that make 'admin'? A package?
I have seen lines similar to these throughout Django, and feel a bit confused.
Thank you!
admin.site is the default instance of the AdminSite class. It is instantiated in django.contrib.admin.sites.
It is then imported in the django/contrib/admin/__init__.py. This makes it available as admin.site when you have done from django.contrib import admin.
When you call admin.site.register(Model), you are calling the register method of this admin site instance.
This is probably something simple, but I'm just not seeing it. So I started a new app called proposals. The following I entered into the app's models.py:
from django.db import models
class Proposal(models.Model):
name = models.CharField(max_length=200)
I then entered the following into admin.py:
from django.contrib import admin
from proposal.models import Proposal
class ProposalAdmin(admin.ModelAdmin):
pass
admin.site.register(Proposal, ProposalAdmin)
This seems incredibly straightforward, right out of the Django docs, but the "Add" and "Change" options for this admin are not appearing. They do appear for everything else, but not this particular admin. Any ideas on what might be causing this?
UPDATE
I'm continuing to check into this, and it's not a permission issue. The permissions are all set to true for add / change / delete. The template that generates the admin page requires a model.add_url for an "Add" link and model.admin_url for a "Change" link. These attributes are missing for some reason. Thus no links. Why they are missing is the big question.
UPDATE
I finally discovered what was going on here. An app which didn't need the add / change links was listed in the installed apps prior to this new one. Whatever it's author did to cause these links not to appear ended up being inherited by all other apps that followed it in this list. When I moved this new app above this one, the links appeared. I didn't even know that it was possible for apps to affect each other like this, but that is something to look for in the future.
I am writing a piece of software and have added an extra field using mezzanines model injection ability's the field is there and can be seen in the database but following the method shown in the docs on this page mezzanine model customisation I cannot get it to show up in my admin page bellow is the code I have written that I believe should work to de-register and then re-register with the new field added to the admin. I am unsure if this code is even running it is in the root of my project as I believe that is where it should be and named admin.py as I believe it should be and have used that file in other projects.
from copy import deepcopy
from django.contrib import admin
from catridge.shop.admin import ProductAdmin
from catridge.shop.models import Product
product_fieldsets = deepcopy(ProductAdmin.fieldsets)
product_fieldsets[0][1]["fields"].insert(-2, "download_file")
class MyProductAdmin(ProductAdmin):
fieldsets = product_fieldsets
admin.site.unregister(Product)
admin.site.register(Product, MyProductAdmin)
Does anyone have any idea as to how this might work I am a bit stumped currently but this is mainly a mezzanine function.
The admin.py file needs to go inside one of your INSTALLED_APPS directories.