Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow create user plans on Django Admin #43

Open
eduardo-matos opened this issue Sep 29, 2014 · 6 comments
Open

Allow create user plans on Django Admin #43

eduardo-matos opened this issue Sep 29, 2014 · 6 comments

Comments

@eduardo-matos
Copy link

Currently there is no way to create a user plan on Django Admin. The user field should be displayed.

This functionality is good for manual registration.

@kevwilde
Copy link
Contributor

Hi there @eduardo-matos, Currently you can manually create a UserPlan by first creating a new User in the admin and then edit his UserPlan which is automatically created after user-creation. Is this what you were looking for?

As an aside: in order for this to work, you need at one plan marked as "default" plan. That way, all created users will immediately be assigned this plan after creation.

@eduardo-matos
Copy link
Author

I have some users that were created before including the django-plans app. The only way I can associate these users with an userplan is by editing via shell, which I was trying to avoid.

My idea was to create a new userplan manually (via admin page), choosing an existing user in the "create userplan" page. Since the "user" field is omitted, There is no way to do that.

@maxpaynestory
Copy link

A quick fix is to override UserPlan inside your admin

from plans.models import UserPlan
from plans.admin import UserPlanAdmin

class UserPlanMyAdmin(UserPlanAdmin):
    fields = ('user', 'plan', 'expire', 'active')

admin.site.unregister(UserPlan)
admin.site.register(UserPlan,UserPlanMyAdmin)

@Alir3z4
Copy link
Collaborator

Alir3z4 commented Jun 28, 2018

Having user editable/writeable on the admin has one annoying con as well, which is when there are many users and simply loading them in a "select" html widget will cause load, especially when there are many users in the database.

If Django admin had a default feature to have a search-friendly select box for related field it would be much easier.

To learn more about this problem you may have a look at https://stackoverflow.com/questions/14912872/django-admin-dropdown-of-1000s-of-users.

To solve such issue there are:

My vote for the second option which is using raw_id_fields in admin panel.
Which gives something like:

image

The solution provided by @maxpaynestory is also an hack that can be done as well, but still will have the same problem that I described.

I am open for any suggestion here and would love to discuss it.

@PetrDlouhy
Copy link
Collaborator

PetrDlouhy commented Jul 20, 2018

For the problem with users created prior introducing django-plans, I would recommend introducing adminaction to the sources. The code might be pretty simple:

for user in UserProfile.objects.filter(userplan=None):
     UserPlan.objects.create(user=user, plan=Plan.objects.get(default=True))

Although there might be other cases, when the manual editation of UserPlans would be needed.

@bezkos
Copy link

bezkos commented Feb 28, 2022

An complete fix here is to use @maxpaynestory solution with Django 2+ autocomplete.

class UserPlanMyAdmin(UserPlanAdmin):
    fields = ('user', 'plan', 'expire', 'active')
    autocomplete_fields = ['user', 'plan']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants