rest api does not list all users - django

I followed the very first page in the tutorial - created UserSerializer and UserViewSet. So, when I go to 127.0.0.1/myapp/ I see a nicely looking Django Rest framework api page. But the problem is, I see an empty list of users:
{}
To solve this problem and to see users in the database I created two new users manually:
user = User(username='Bob',password='Bob')
user.save()
user = User(username='John',password='John')
user.save()
And if I connect to sqlite and select from auth_user table, I see two rows corresponding to Bob and John, but if I refresh 127.0.0.1/myapp/, I still see an empty list of users {}. I do not know how to solve this.
EDIT
I created a user another way:
User.objects.create_user(username='admin', password='admin')
and now I'm able to login, but still when I go to 127.0.0.1/myapp/ I see {}.
EDIT
I even visited admin page and created a new user. But {} still remains. It seems as if the very first "most simple" official example does not work. I can not list users and see them in my app.

Example uses users endpoint
router.register(r'users', UserViewSet)
urlpatterns = [
url(r'^', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
You are using myapp endpoint, and not telling us how you hooked it up in your urls.py.

Related

In django admin panel permissions given to the users by group not working

In Django admin panel
I create groups and give permission to them.
eg:
Create a Student Group and give it permission to view the student model.
Create a Teacher Group and give it permission to view the student model and add the student model.
create user using API and added to those groups,
and also staff status checked for each user.
When users log in on the admin panel that shows Site administration You don’t have permission to view or edit anything.
How to solve it.
I've got the same issue, tried every solution out there but they're all either outdated or not useful to my case. First let me ask you some questions.
1- What version of django are you using?
2- Do you have ModelBackend set in your AUTHENTICATION_BACKENDS like that?
AUTHENTICATION_BACKENDS = [
...
"django.contrib.auth.backends.ModelBackend",
...
]
3- If you're using a custom user model, have you extended PermissionsMixin in your class like that?
class CustomUser(AbstractBaseUser, PermissionsMixin):
# Fields
If you've done all of that and still stuck at it, like myself, then i'm not really sure what could help us. Anyway, i'll keep you posted if i get to know anything else.

How to edit the Django Admin User social auths listing page?

I am having trouble finding where this admin file exists so I can add an extra field. I think it's auto-magically created upon setup.
I want to add a date field, specifically, to the listing page (shown below), perhaps after the UID field so I can know when the user auth was created.
screenshot of django user social auths listing page
Okay here's what I've tried using Django-allauth and I think it somehow works the same with django-socialauth. Just get the gist of the idea and work it to your code
Extend first the SocialAccountAdmin in any of your admin.py files, better if in a specific app like "user", "home", or whatever you prefer.
admin.py
from allauth.socialaccount.admin import SocialAccountAdmin
from allauth.socialaccount.models import SocialAccount
class MySocialAccount(SocialAccountAdmin):
list_display = ('user', 'uid', 'provider', 'date_joined') # I haven't tried just adding a certain list to the list_display, for the meantime add all necessary fields just like how socialauth did
admin.site.unregister(SocialAccount) # Need to unregister the default socialaccount admin
admin.site.register(SocialAccount, MySocialAccount) # Then register it back with the custom made admin
There may be perhaps a better way to do this but this did the work.
Can it be interesting to just add a field to your model ? Adding a DateField for your creation date. Probably you need to understand learn more with : https://docs.djangoproject.com/en/3.0/ref/models/fields/

Generic django admin per client

In my django project, i would like to use Django Admin for multiple organization. So in django urls system, i try to use a system like that :
urlpatterns = [
path('<organization>/admin', admin.site.urls)
]
With that syntax,on a simple example (default startproject+setup), i got an 'NoReverseMatch' :
Reverse for 'logout' with no arguments not found. 1 pattern(s) tried: ['(?P<client>[^/]+)\\/admin\\/logout\\/$']
At the end i try to implement a system like that:
Basic auth user (maybe change backend for email)
Organization:
Organization_User (inherit from basic auth or relationship)
Organization_Area (manyTomany with Organization_User)
and for urls i don't know if syntax bellow works:
www.example.com/admin => admin SuperUser only
www.example.com/organization1/admin => Admin for Organization 1
www.example.com/organization2/admin => Admin for Organization 2
I'm asking myself, is it possible to do that only with Django Admin system ?
For example, use one Admin and try URL filtering or use AdminSite inheritence
Best Regards,
Could you just assign users to groups depending on which organisation they belong to? Then alter the groups permissions so they can only see the parts you wish them to have access to.

admin specific app in django

I'm using Django 1.11
I want to create an admin specific app which is only accessible by admin interface.
I want to create an app to add country and state records which can be added or edited only by Admin from /admin.
Also, these records can be used by any application to populate a select field in adding a record like address.
If I create country app in mydangoapp/country and add URL to mydangoapp/mydangoapp/urls.py then it can be accessed by other users too by www.example.com/country which I don't want.
Where to create a model for this? Is it possible to create an admin specific app only?
Every app can be used only as admin specific till the time you don't give a urlpatterns for that app to be accessed by users.
So, you just create an app, make models, register them. But don't write views, forms and urls for that app.
In your case you should do something like that in your mydjangoapp urls.py:-
from country import views as country_views
urlpatterns = [
url(r'^admin/custom_prefix/country/', include('country.urls')),
url(r'^admin/', admin.site.urls),
]
Make sure u enter it in this order(i.e. before the r'^admin/' regex) in urlpatterns list.
Hope this solves your issues :-)

Get the site_id of the user_id in Django?

I'm really struggled by this. I looked through the Django documentation and I couldn't find a method to get the site_id of the currently logged in user_id with Sites framework.
I want to build a blogpost form and I wan't to include the site_id so that the content that the user publish will go to that particular site_id.
The user will only have permissions to publish to one site_id.
To clarify:
Publish content to the site_id that the user_id belongs to.
Is there a way to do this?
A user does not belong to a site. A request does.
If, somehow, in your Django project sites are owned by users, then you should add a new field to your User model. Something like this:
class MyUserModel(...):
...
blog = models.OneToOneField(Site)