Django admin checkbox with multiple select - django

I have a Django app. Pretty basic one at that.
In the model I have a class for items and a class for groups. The groups has a many to many for the items:
items = models.ManyToManyField(item, verbose_name="list of items", max_length=100000, blank=True)
When I add this to the admin section I would like to have a checkbox with multiple select. Is this possible. All of the solutions I have looked at don't sow it in the context of an admin page too. The Django admin page is all I need it to work on as I am not making any custom, public facing pages.
What is the easiest and most simple solution to replace the multiple select box with a multiple checkbox.
PS. I am relatively in-experienced with Django so I need to see what I need to import in the model and admin.
Thanks

If you know how to do it for a standard modelform, you know how to do it in an admin page too, as they are based on normal forms.
Just define the form as normal, then tell the admin to use it for your model:
class MyModelAdmin(admin.ModelAdmin):
form = MyFormWithTheMultipleSelect

way based on overriding admin templates
/myproject/templates/admin/myapp/mymodel/change_form.html
{% extends "admin/change_form.html" %}
{{ block.super }}
<script type='text/javascript' src='/media/js/jquery.js'></script>
<script>
$(document).ready(function(){
myselect = $("#id_M2M_FIELD_NAME");
// here you manipulating with your multiple select,
// and convert it to checkboxes, or something else.
})
</script>
{% endblock %}

Related

How to create Django_filter in Checkbox view on my website front panel?

I am using django_fiter package for filter data, but it's displaying filter in the input box or some filter in the dropdowns, but I want all filters in the checkbox (like as Django default admin panel filter), please let me know how I can convert it in checkbox filter on my website front panel.
Here is my code for filter view form...
<form method="get">
{{ filter.form|bootstrap }}
<input type="submit" />
</form>
and here is my views.py file code...
class ProductFilter(django_filters.FilterSet):
name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Product
fields = ['saleprice', 'title','veg_non','brand','supplement']
it's looking like this...
and I want in this format, please let me know how I can customize it.

How to store social usernames/links in Django?

I have a personal blog written with Django. I want to make my social links/usernames dynamic so I can change them whenever I want.
I'm doing this with charfields. I'm adding new charfield for each social username/link field then my template's getting these usernames to show them in a tags. It's okay but I think it's not the easiest way.
Am I wrong or there is a better way to do this?
Maybe it would be easier to create a model for it. That way you would be able to add/remove social links without having to touch the code.
from django.conf import settings
from django.db import models
class SocialProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='social_profiles')
network = models.CharField(max_length=100)
username = models.CharField(max_length=100)
url = models.URLField(max_length=500)
Then if you are using Django Admin, you could add this model as a TabularInline of the User model for example.
In your templates you could render the links dynamically:
{% for profile in request.user.social_profiles.all %}
{{ profile.username }}
{% endfor %}
Something like that.

adding code for a Django admin field

I have a tinymce textarea in my django admin and I need users to be able to upload images (via AJAX) that will be linked in this textarea.
That would be accomplished by adding an "Image Upload" button (it's already working) in the top of the "Content" textarea.
What's the recommended way of doing that?
I can think on 2 solutions:
extending change_form and replacing {% for fieldset in adminform %} for the actual fields... and when it's the content field, I add this value
dynamically adding this button with javascript (find out where the content field is and add a <div> before it)
A better solution, if possible, would be to override just this specific field in the admin templates. Is that possible? Or are there better solutions?
PS: this field is not part of the DB (it just uploads one or more images, saves it to the storage and returns a link that will be included on the tinymce).
maybe you could use this
formfield_overrides from the djangodocs
if not, create a subclass of modelform and overide the form of admin with ModelAdmin.form.

How to add custom control to django admin site?

Hey, I want to add custom buttom to the django admin site, and the users must be administrators, I do not expect you tell me how to do in each step, but please brief write the right approach, if you also can provide some reading URLs, that's gonna be awesome.
http://docs.djangoproject.com/en/dev/intro/tutorial02/ - "Customize the admin form" shows how to modify the admin section of an app.
http://docs.djangoproject.com/en/dev/topics/auth/ - "get_group_permissions()" will allow you to get the group permissions of a user. "has_perm()" returns true for a single permission.
http://docs.djangoproject.com/en/dev/howto/custom-management-commands/ - how to customize django management
http://docs.djangoproject.com/en/dev/ref/contrib/admin/ - "ModelAdmin" can be used to specify a template for the admin site
Using these, you can put together a custom template with any custom controls and only show them if the user has a specific permission.
You can copy from django /django/contrib/admin/templates/admin/base.html (or base_site.html) to your project /templates/admin/base.html then customize base.html
This part {% block footer %}<div id="footer"></div>{% endblock %}
Also this peace of template could help {% if user.is_active and user.is_staff %}

Dynamic User Menu in Django

Is there a way to have a user menu that changes according to the permissions assigned to the user group a user belongs to? I am thinking of something that checks for these permissions at the view level, and also removes menu options a user does not have permission to.
Yes it is possible to access the user object in the template and check if the user is staff like this:
{% if user.is_staff %}
<li>
Admin
</li>
{% endif %}
This would be an example where your menu where li items of links. The admin link would only be rendered for users with is_staff status. The same could be done with is_authenticated.
Django is build to have logic and presentation separated, so if you want to do some more fine grained control of the menu, I would suggest doing the logic inside of the view, and then set a variable that you can check in the template to determine which menus to show.
For the most part, django's admin already doesnt give you links to things you can't do.
Django grappelli (a django admin skin) implements some sort of bookmarking, if that is what you mean http://code.google.com/p/django-grappelli/