Update a particular field in the entire django model - django

I have a model
class College(models.Model):
is_published = models.NullBooleanField(default=False)
name = models.CharField(max_length=255)
I registered the model in admin.py
#admin.register(College)
class CollegeAdmin(admin.ModelAdmin):
list_display = ('id', 'name')
Now I want to have a button on the admin panel, on pressing which I can change the is_published field of all the objects in the model to True.
I have no idea how should I proceed. Any working code snippet will be appreciated.

A custom admin action is the perfect tool for this job.
For example (copied almost word for word from the documentation):
def publish(modeladmin, request, queryset):
queryset.update(is_published=True)
publish.short_description = "Mark selected stories as published"
#admin.register(College)
class CollegeAdmin(admin.ModelAdmin):
list_display = ('id', 'name')
actions = [publish]

Related

Display a list of related fields in the admin panel

I have two related models.
class Refbook(models.Model):
code = models.CharField(max_length=100, unique=True)
name = models.CharField(max_length=300)
description = models.TextField()
class VersionRefbook(models.Model):
refbook_id = models.ForeignKey('Refbook',
on_delete=models.CASCADE,
related_name='versions')
version = models.CharField(max_length=50)
date = models.DateField()
When I edit a Refbook instance in the admin panel, I want the read-only list of available versions of this Refbook instance to be displayed on the same page.
I know that it is possible to output them through TabularInline . And it seems that there is a read-only property here.
Maybe there is a way to display just a list in a column or row separated by commas?
Now, I have this code in admin.py:
#admin.register(Refbook)
class RefbookAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'name']
I tried to create a "get_versions(self)" method in models.py in Refbook class, in which I received a queryset using related_name. But I can't display it in the admin panel. Or is it still correct to do this using the model.ModelAdmin parameters?
You can do something like this:
#admin.register(Refbook)
class RefbookAdmin(admin.ModelAdmin):
list_display = ['id', 'code', 'name', 'versions']
def versions(self, obj):
return ', '.join(obj.versions.values_list('version', flat=True))

How can I customize filtering in DJango Admin?

admin.py
class authUserMenu(admin.ModelAdmin):
list_display = ["__str__", "user_id", "menu_id","is_active"]
class Meta:
Model = AuthUserMenu
admin.site.register(AuthUserMenu, authUserMenu)
models.py
class AuthUserMenu(models.Model): # USER VS MENU relation
user = models.ForeignKey(AuthUser,on_delete=models.DO_NOTHING,blank=True, null=True)
menu = models.ForeignKey(Menu,on_delete=models.DO_NOTHING,blank=True, null=True)
is_active = models.BooleanField(default=False)
class Meta:
db_table = 'auth_user_menu'
ordering = ['user','menu','is_active']
def __str__(self):
# return self.id
return str([self.user.username,'>>>>>>', self.menu.menu_name])
In my Django admin panel
When filtering with username should only show some menus under some condition... How can I achieve this?
Suppose here for the username 4 menu is showing. But it should show 2 menu. This may obtain by db querying.
This is how A user related to menus
You need some JS to call a view asyncronously every time the user field is updated and change the options in the menu field with the response. The package django-autocomplete-light is a popular choice for exactly this, especially updating field values from other fields is relevant
forms.py
class AuthUserMenuForm(forms.ModelForm):
class Meta:
model = AuthUserMenu
fields = '__all__'
widgets = {
'menu': autocomplete.ModelSelect2(
url='menu-autocomplete',
forward=['user']
)
}
views.py
class MenuAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
qs = Menu.objects.all()
user = self.forwarded.get('user', None)
if user:
# Not clear exactly what your filter would be here so left it but you have user available
qs = qs.filter(...)
return qs
urls.py
urlpatterns = [
...
path('menu-autocomplete/', views.MenuAutocomplete.as_view(), name='menu-autocomplete'),
...
]
class AuthUserMenuAdmin(admin.ModelAdmin):
form = AuthUserMenuForm
list_display = ["__str__", "user_id", "menu_id", "is_active"]
admin.site.register(AuthUserMenu, AuthUserMenuAdmin)
Sorry But the filter's you are applying for admin is only valid for how they are Going to look on the admin panel it won't customize your model properties and in order to achieve that you need to create a filter as eg ...
user = self.get_object()
articles = Article.objects.filter(author_id)
Something like that

display only some fields in get api response django serializer

I have an example model which has a fk relation with user model and Blog model. Now I have a get api which only requires certain fields of user to be displayed.
My model:
class Example(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
null=True,
related_name="user_examples",
)
blog = models.ForeignKey(
Blog,
on_delete=models.CASCADE,
null=True,
related_name="blog_examples",
)
/................./
Now my view:
class ExampleView(viewsets.ModelViewSet):
queryset = Example.objects.all()
serializer_class = ExampleSerializer
def list(self, request, *args, **kwargs):
id = self.kwargs.get('pk')
queryset = Example.objects.filter(blog=id)
serializer = self.serializer_class(queryset,many=True)
return Response(serializer.data,status=200)
My serializer:
class ExampleSerializer(serializers.ModelSerializer):
class Meta:
model = Example
fields = ['user','blog','status']
depth = 1
Now when I call with this get api, I get all example objects that is required but all the unnecessary fields of user like password, group etc . What I want is only user's email and full name. Same goes with blog, I only want certain fields not all of them. Now how to achieve this in a best way??
You will have to specify the required fields in nested serializers. e.g.
class BlogSerializer(serializers.ModelSerializer):
class Meta:
model = Blog
fields = ['title', 'author']
class ExampleSerializer(serializers.ModelSerializer):
blog = BlogSerializer()
class Meta:
model = Example
fields = ['user','blog','status']
are you setting depth in serializer's init method or anywhere else? beacause ideally it should only display id's and not anything else. if yes then set depth to zero and use serializer's method field to return data that you need on frontend. I can provide you with example code samples

Display information about linked models fields in the django admin

These are my models:
class Partner(models.Model):
name = models.CharField(max_length=200, verbose_name="Organisation name")
class ResearchActivity(models.Model):
title = models.CharField(max_length=200)
partner = models.ManyToManyField(ActivityPartner, blank=True)
I'd like, in the Django administration forms, to have a field in my Partner edit form representing the ResearchActivity linked to that Partner.
Can this be achieved by adding a field to my Partner model (say, naming it linked_partner) and then edit my admin.py like so:
#admin.register(ActivityPartner)
class ActivityPartnerAdmin(admin.ModelAdmin):
search_fields = ['academic',]
autocomplete_fields = ['partnership_type', 'relationship_type', 'academic_links']
def get_changeform_initial_data(self, request):
return {'live_contract': ResearchActivity.objects.all().filter(linked_partner__id=request.ResearchActivity.partner.id)}
?
I have just come across in the display() decorator, new from Django 3.2. With it, I can simply do:
#admin.register(ActivityPartner)
class ActivityPartnerAdmin(admin.ModelAdmin):
search_fields = ['academic',]
autocomplete_fields = ['partnership_type', 'relationship_type', 'academic_links',]
readonly_fields = ('get_ra',)
#admin.display(description='Live contract(s)')
def get_ra(self, obj):
return list(ResearchActivity.objects.filter(partner=obj.id))
to achieve what I want.
If I also wanted to edit those ManyToMany relations, I can use the inlines option:
class LiveContractsInline(admin.TabularInline):
model = ResearchActivity.partner.through
#admin.register(ActivityPartner)
class ActivityPartnerAdmin(admin.ModelAdmin):
inlines = [
LiveContractsInline,
]

How to change the selectable options in ManyToMany field?

I'm developing an application where I have a User creating a Document with some general content. I set up the model so the user can share that document with other users through a ManyToMany field shown below. The problem I have is the ManyToMany field shows all the users on my site as possible collaborators - whereas I want to only show them their team members. How would I go about doing that?
My models:
class Document(models.Model):
...
collaborators = models.ManyToManyField(User, related_name="doc_collaborators")
class User(models.Model):
...
team = models.CharField('team', max_length=50)
My forms:
class CreateDocForm(forms.ModelForm):
class Meta:
model = Document
exclude = ('created_at', 'updated_at', 'owner', 'slug')
One solution might be to override the queriset when the form is initialized:
class CreateDocForm(forms.ModelForm):
...
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['collaborators'].queryset = User.objects.filter([some filters])
...
Another solution is to use third-party modules: django-autocomplete-light