ModelAdmin.autocomplete_fields looks to be very simple to implement into the Django admin:
class UserAdmin(admin.ModelAdmin):
autocomplete_fields = ['material']
admin.site.register(User, UserAdmin)
class MaterialAdmin(admin.ModelAdmin):
search_fields = ['name']
admin.site.register(Material, MaterialAdmin)
It renders the field correctly (as a search field instead of a dropdown), but the search field says "The results could not be loaded" and inspect shows:
*/admin/autocomplete/ 403 (Forbidden)
jquery.js:9203
I assume there is a csrf issue receiving data from the Material model. I looked into ways to exempt this request from csrf but couldn't figure out how to do it through ModelAdmin.autocomplete_fields.
I tried using django-autocomplete-light as well and couldn't get it working.
That's because of the conflict between DAL and Django 3.2 + versions. If you turn DAL off it can solve this problem. DAL's js overloads Django's one and that's it. To know more just follow the link to
Dal's github issue
If you implement your own UserAdmin either make sure search_fields is defined, as requested in the documentation, or use Django's UserAdmin as a base class.
Also, if you just upgraded your Django version and it stopped working, clear your cache to load the proper javascript files.
Related
I'm trying to perform a simple insert operation using Mongoengine and Django.
Regarding my project structure simply I have a project, AProject and an app, AnApp. I have a running mongo in a remote machine with an IP of X.X.X.X. I am able to insert document using Robomongo in it.
I have removed the default Database configuration part of the settings.py located inside the AProject directory. The newly added lines are shown below:
# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
import mongoengine
# ----------- MongoDB stuff
from mongoengine import register_connection
register_connection(alias='default', name='AProject', host='X.X.X.X')
# ----------
Now let me show the models.py and the views.py located inside AnApp.
models.py
from mongoengine import Document, StringField
class Confession(Document):
confession = StringField(required=True)
views.py
from django.http import HttpResponse
from models import Confession
from mongoengine import connect
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession()
confession.confession = 'First confession from the API'
print(confession.confession + ' Printable') # The output is --First confession from the API Printable--
print(confession.save()) # The output is --Confession object--
return HttpResponse(request)
The urls.py located inside AProject is simply as below:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^confessions/', include('confession.urls')),
url(r'^admin/', admin.site.urls),
]
When I enter http://127.0.0.1:10000/confessions/ I see a blank screen which I expect. However there is nothing saved from the API. I get the expected output except Confession object.
How can I solve this problem?
EDIT:
I found the concrete proof that currently MongoEngine's support for Django is unstable and it is corresponding to Django version 1.9:
MongoEngine documentation on Django support states:
Django support has been split from the main MongoEngine repository. The legacy Django extension may be found bundled with the 0.9 release of MongoEngine.
and
6.1. Help Wanted!
The MongoEngine team is looking for help contributing and maintaining a new Django extension for MongoEngine! If you have Django experience and would like to help contribute to the project, please get in touch on the mailing list or by simply contributing on GitHub.
Which leads to this repo, where the following is stated:
THIS IS UNSTABLE PROJECT, IF YOU WANT TO USE IT - FIX WHAT YOU NEED
Right now we're targeting to get things working on Django 1.9
So it may be possible that it cannot play well with Django at this state or with your version and thus the problem occurs.
Initial attempt, leaving it here for legacy reasons.
I believe that the problem occurs on how you are initializing your object, although I do not have a set up to test this theory.
It is generally considered that is better to make a new object with the .create() method:
def index(request):
connect('HacettepeItiraf', alias='default')
confession = Confession.objects.create(
confession='First confession from the API'
)
print(confession.confession + ' Printable')
confession.save()
return HttpResponse(request)
Have a look at the Django & MongoDB tutorial for more details.
In this tutorial, the supported Django version is not mentioned, but I haven't found concrete proof that MongoDB Engine can or can't play well with Django version > 1.8.
Good luck :)
I have a problem with the Django Rest Framework and permissions. DRF won't let me have more than one permission on my views for example.
If I login to the API as an admin user I can get access using this mixin:
class PermissionMixin(object):
permission_classes = (permissions.IsAdminUser)
Now, if I add a second permission mixin:
class PermissionMixin(object):
permission_classes = (permissions.IsAdminUser, TokenHasReadWriteScope)
Admin users are denied access. What should happen is both admin user and users with a token get access, however with above now only TokenHasReadWriteScope users have access.
Has anyone else had this issue, whats going on here?
I need both type of users to have access.
This is how my view looks:
class SomeList(PermissionMixin, generics.ListCreateAPIView)
queryset = Award.objects.all()
serializer_class = AwardSerializer
PS TokenHasReadWriteScope is from django-oauth-toolkit
With version 3.9 and above of Django Rest Framework, they have built-in support for composable permission classes and you can use and/or-operators out of the box:
permission_classes = [IsAuthenticated & (ReadOnly | IsAdmin)]
The behavior you are experiencing is absolutely normal, that's how DRF was designed. If you want to have at least one of those permission classes, you need to specify a more 'complex' condition. This is a very good example of what you might use. After you install it, you can use it like this:
from rest_condition import Or
...
permission_classes = (Or(permissions.IsAdminUser, TokenHasReadWriteScope),)
I've followed the tutorial and the examples for django-facebook from https://github.com/tschellenbach/Django-facebook/tree/master/facebook_example
I've got it up and running (as in i've run syncdb and migrations, and integrated the javascript and css code into my template).
When I click the link in my template, it successfully connects to Facebook and I can authorize. However, when it redirects, I get an error:
Request Method: POST
Request URL: http://127.0.0.1:8000/facebook/connect/?facebook_login=1
Django Version: 1.5.1
Exception Type: AttributeError
Exception Value: user or profile didnt have attribute facebook_id
Exception Location: /usr/local/lib/python2.7/dist-packages/django_facebook/utils.py in get_user_attribute, line 109
Python Executable: /usr/bin/python
Python Version: 2.7.3
This is within django-facebook's code, and I'm not sure if I've done something wrong with my setup. I've created my own UserProfile model which extends the abstract FacebookProfileModel class (I've tried using FacebookModel as well):
from django_facebook.models import FacebookProfileModel
from django.db.models.signals import post_save
class UserProfile(FacebookProfileModel):
user = models.OneToOneField('auth.User')
def create_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
post_save.connect(create_profile, sender=User)
I can see the profile get created in the db, although it's all nulls (except for the user id used in the foreign key). Not sure what I'm doing wrong, any pointers?
I found out what was wrong, and want to post the solution back. The problem was that I didn't specify AUTH_PROFILE_MODULE in my settings.py. In the django-facebook documentation, it didn't mention that when it gave the option to extend the profile class, so I missed it. Silly mistake!
I have a custom user model (it's actually named User as I didn't see any need to name it otherwise) in my Django 1.5c1 project (currently running on the latest from the Django 1.5 branch on github). AUTH_USER_MODEL is defined in my settings properly, so the auth module works correctly and I can log in etc. fine.
However, with the custom user module enabled, the admin site doesn't work. When I add admin.autodiscover() to my urls.py, every page on the site (not just admin pages) throws a NotRegistered exception and says The model User is not registered. The traceback shows that admin.autodiscover() is trying to call admin.site.unregister(User), apparently before it has registered that model.
I tried renaming my user model to something other than User, but it didn't seem to work. I also tried creating my own admin.py for that app, and then I tried manually registering my custom User model with the custom UserAdmin model specified in admin.py before admin.autodiscover() ran, but that actually caused a separate exception saying that User was already registered.
What should I try next in order to get admin.autodiscover() working?
It looks like you need to jump through a few extra hoops if you want your custom User model to work with the admin. From the documentation:
...your User model must define some additional attributes and methods.
These methods allow the admin to control access of the User to admin
content:
class models.CustomUser
is_staff True if the user is allowed to have access to the admin site.
is_active True if the user account is currently active.
has_perm(perm, obj=None) True if the user has the named
permission.
has_module_perms(app_label) True if the user has perm
to access models in the given app.
I set up a brand new empty project with a custom user model and attempted to recreate the situation, which led to a diagnosis: we had added the django-usertools package to the project, which has not been updated for Django 1.5 and apparently conflicts with custom user models. Removing that package from the installed apps list in settings resolved the issue.
get_absolute_url() method is cool, but in some cases is not needed. django.contrib.auth.models.User has it set by default, this cause my projects to have a broken link in the admin.
How can I prevent that from happening?
In one of my old projects I set a custom template in which I removed the html of the button, it doesn't sound like a good solution that would scale though. Anything better?
If you click on Django 1.7 link, the site will tell you that "Its an insecure version of Django that is no longer supported. Please upgrade to a newer release!"
For Django 1.9, following solution works fine as mentioned in the Django documentation
in myapp/admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
# Disable View on Site link on admin page
site_url = None
This can be done, per model, as of django 1.7.
# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
view_on_site = False
admin.site.register(MyModel,MyModelAdmin)
Instead of monkey patching you can hide the button on the client side using JavaScript. The HTML of the view on site button looks like this:
<li>View on site</li>
If you just hide the anchor tag you will get part of the round button appearing as that is applied on the li tag. Now unfortunately there is no easy way to use css to select that specific li tag since it doesn't have a class, name or id on it. So we can use jquery which gives you more control on your selectors. Put the following in your static folder. For example in the location static/admin/user_change_form.js
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
});
Your admin.py could then look something like this:
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.admin import site
class CustomUserAdmin(UserAdmin):
class Media:
js = ['admin/user_change_form.js']
site.unregister(User)
site.register(User, CustomUserAdmin)
UPDATE
A feature was added in Django 1.7 ModelAdmin.view_on_site which lets you not display the “View on site” link.
I know this is old but I came across it when I had to do the same thing.
A better solution would be to monkey patch it in an accounts/admin.py file when you have accounts in INSTALLED_APPS.
admin.site.unregister(User)
# We don't want a broken View on Site link. Thanks for that, contrib.auth!
del User.get_absolute_url
admin.site.register(User, MyUserAdmin)
Django 2.0 above you can add in default admin
admin.site.site_url = None
Above trick worked for me very well.
As a last resort, I have a monkey_patch app at the bottom of my INSTALLED_APPS which modifies the built in django contrib apps in ways I haven't found better ways to modify such as username length, default admin forms, __unicode__, etc.
Just watch your back when you upgrade django / in general.
from django.contrib.auth.models import User
del User.get_absolute_url
I'm using Django 1.4 and Marwan Alsabbagh's solution worked fine for me. Although, when opening/refreshing User change form there was a short blink. That is because JQuery hides this button only when page is loaded.
To solve this minor issue I used CSS to hide the whole .change-form block. After page is loaded this block's visibility is restored by means of JQuery. So, my code looks like this:
admin.py:
class Media:
js = ['js/admin/user_change_form.js']
css = {'all': ('css/admin/user_change_form.css',)}
...static/css/admin/user_change_form.css
.change-form {
visibility: hidden;
}
...static/js/admin/user_change_form.js
/* Do not show 'View on site' button in User change form */
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
/* restore visibility of the page (hidden in css to avoid blinking) */
$(".change-form").css('visibility', 'visible')
});
use
admin.site.site_url = "your url here"
in url.py of ur main app to modify the "visit site" on django page
and
for "view_on_site" removal
use
view_on_site = False in ur class with display_list for
Inside your app config (apps.py) do this:
class MyAppConfig(AppConfig):
def ready(self):
admin.site.site_url = None
Works in Django 4.0 as well.