Filter queryset based on related object's field value

Advertisements I have two models: class PartUse(models.Model): … imported = models.BooleanField() class PartReturn(models.Model): partuse = models.ForeignKey(PartUse) … imported = models.BooleanField() class PartUseListView(ListView): model = PartUse def get_queryset(self): if self.request.GET.get(‘show_imported’, None): qs = self.model.objects.all() else: qs = self.model.objects.filter(Exists(PartReturn.objects.filter( imported=False, id__in=OuterRef("partreturn")))) return qs I want to filter QuerySet for PartUse to return all PartUse instances that have… Read More Filter queryset based on related object's field value

Admin page shows only one model field of many

Advertisements models.py from django.db import models class items(models.Model): price = models.IntegerField(), name = models.TextField(max_length=100), category = models.CharField(max_length=100) When i try to insert a data on admin page it is showing only category field and I’m can fill only this one field. Also it is showing an extra s in table names for no reason. I… Read More Admin page shows only one model field of many

How to print an object of a foreignkey instead of the entire class to Django administration

Advertisements In my ‘models.py’ file, I have 2 LOOKUP tables: the ‘Part’ class and the ‘Vendor’ class as shown: # Part Lookup table class Part(models.Model): part_id = models.CharField(max_length=64) # Part ID description = models.CharField(max_length=64) # Part description pq = models.DecimalField(max_digits=7, decimal_places=2) # Pack quantity mrrp = models.DecimalField(max_digits=10, decimal_places=2) # Manufacturers Recommended Retail Price # Display… Read More How to print an object of a foreignkey instead of the entire class to Django administration

Filter by a virtual name in the admin site

Advertisements Django4.1 class Exercise(NameMixin, UrlMixin, PriorityMixin, CommentMixin): unit = models.ForeignKey(‘vocabulary_units.Unit’, on_delete=models.CASCADE, null=True, ) class Phrase(models.Model): exercise = models.ForeignKey(‘vocabulary_exercises.Exercise’, on_delete=models.CASCADE, null=True, blank=False) @property def unit(self): result = self.exercise.unit return result A phase belongs to an exercise, excercise belongs to a unit. The problem is that in the admin site I’d like to organise for phrases a… Read More Filter by a virtual name in the admin site

How to give specific users specific persmissions to view and edit Specific Areas in Django admin

Advertisements I’m making a django website whose admin page looks like this I want users(The content writer) to access only the Main Area(Events, Formats, Organisers) and not the whole thing. I haven’t made any such user as Content Writer as of now. What are the permissions that should be given to that user. Should I… Read More How to give specific users specific persmissions to view and edit Specific Areas in Django admin

Django admin – prevent changing a field after it has become true

Advertisements I have a model registred in admin.py: class OrderAdmin(admin.ModelAdmin): list_display = (‘org_name’, ‘address’, ‘total_cost’, ‘phone’, ‘data_time’, ‘is_called’, ‘is_payed’) search_fields = (‘org_name’, ‘phone’) list_filter = (‘data_time’, ‘total_cost’, ‘data_time’) list_editable = (‘is_called’, ‘is_payed’) readonly_fields = (‘data_time’, ‘user’, ‘total_cost’) inlines = [OrderItemsAdmin, ] I need to do something like: class OrderAdmin(admin.ModelAdmin): list_display = (‘org_name’, ‘address’, ‘total_cost’, ‘phone’,… Read More Django admin – prevent changing a field after it has become true

Django admin – admin inheritance

Advertisements When registering django admin, I inherited another admin, but I don’t know why the model of list_display is changed. Let me explain the situation in detail with code. @admin.register(User) class UserAdmin(models.Model): list_display = [a.name for a in User._meta.concrete_fields] @admin.register(Sales) class SalesAdmin(UserAdmin): pass According to the code above, SalesAdmin Admin appears to inherit from UserAdmin.… Read More Django admin – admin inheritance

why Django admin short_description function not working?

Advertisements I am trying to make some changes in my django admin panel such as want to show "title" instead of "blog_tile" but I am not understanding why changes not reflecting. class BlogAdmin(admin.ModelAdmin): readonly_fields = [‘blog_publish_time’, ‘blog_update_time’] list_display = [‘blog_title’, ‘blog_status’, ‘blog_publish_time’, ‘blog_update_time’] def rename_blog_title(self, obj): return obj.blog_title[:10] rename_blog_title.short_description = "title" admin.site.register(Blog, BlogAdmin) where I… Read More why Django admin short_description function not working?