how to change admin field name in django, python - python

I added one model Author in models.py file in my app and created model names for the author while I opened in admin panel it's showing as Author object(12) how can I change that?
I tried to add Unicode
class Author(models.Model):
author_name=models.CharField(max_length=300)
I want field name instead of Author object in the admin panel.
below i want change Author Object

Try This:
class Author(models.Model):
author_name=models.CharField(max_length=300)
def __str__(self):
return self.author_name
Follow what #dirkgroten said "Make it a habit to always override str for all your models"
Also You can use list_display method in your admin.py to achieve similar result. Create a admin class and use list_display to render fields of model in tabular format
Admin.py
from app.models import Artist #<-----Import you artist model
#admin.register(Artist) #<----- admin class should be just below this line
class ArtistAdmin(admin.ModelAdmin):
list_display = ["id", "author_name"]
Or you can also do this:
from app.models import Artist #<-----Import you artist model
class ArtistAdmin(admin.ModelAdmin):
list_display = ["id", "author_name"]
admin.site.register(Artist, ArtistAdmin) #<----register your class also

You can overrride __str__ method in django model class like that
class Author(models.Model):
author_name=models.CharField(max_length=300)
def __str__(self):
return self.author_name

Here is the example of overriding __str__ method for cases like yours.
class Language(models.Model):
language = models.CharField(max_length=32)
class Meta:
app_label = "languages"
ordering = ["-modified"]
def __str__(self):
return f"{self.language} (language {self.id})"

Related

How to get value of model method in Django Rest Framework?

So basically I have a django model that has a ManyToManyField of friends and two methods that run on it. Here are my files:
Models.py:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
first_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50, blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
friends = models.ManyToManyField(User, blank=True, related_name='friends')
def friends_list(self):
return self.friends.all()
def number_of_friends(self):
return self.friends.all().count()
Serialzers.py:
from rest_framework import serializers
from .models import Profile
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = '__all__'
Views.py:
from rest_framework import viewsets, permissions
from .models import Profile
from .serializers import ProfileSerializer
class ProfileViewSet(viewsets.ModelViewSet):
queryset = Profile.objects.all()
permission_classes = [
permissions.AllowAny
]
serializer_class = ProfileSerializer
The issue is that in the Api, the return values of the method aren't there. The friends_list method for example is supposed to return a list of friends you have and even though this does work in a traditional django project, the Django Rest Framework is not showing any value for this method. How can I fix this and get the return values for both methods to show up in the api?
Since the model serializer picks up only model fields for the serializer fields, you won't automatically get any methods copied over.
You can still send this read only data over the API by explicitly adding the two fields with reference to the model methods
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = [
# need to explicitly define all fields I believe
'friends_list',
'number_of_friends',
]
Now that the two fields (matching the method name are declared, DRF should create SerializerMethodField or ReadOnly field (not sure which one, but they are similar) for each of them.
It works coz it sets the source for those fields to be the same name, and if finds some attribute (in this case the methods) on the model.
If that doesn't work, you can
class ProfileSerializer(serializers.ModelSerializer):
friends_list = serializers.SerializerMethodField()
number_of_friends = serializers.SerializerMethodField()
class Meta:
model = Profile
fields = [
# need to explicitly define all fields I believe
'friends_list',
'number_of_friends',
]
def get_friends_list(self, instance):
return instance.friends_list()
def get_number_of_friends(self, instance):
return instance.number_of_friends()
when you use __all__ it call fields only you have to call fields with methods using list like that
`fileds = ["first_name","last_name","user",
"friends","friends_list","number_of_friends"
]`

Django related model not updating related object in admin

I have 2 models that look like this:
models.py
class Client(models.Model):
deal = models.ManyToManyField('Deal', related_name="clients")
class Deal(models.Model):
client = models.ManyToManyField(Client, related_name="deals")
Then in the admin, I have inlined the related models to make it easy to make changes regardless of the object type you have open.
admin.py
class ClientInline(admin.TabularInline):
model = Deal.client.through
class DealAdmin(admin.ModelAdmin):
inlines = [ClientInline]
class DealInline(admin.TabularInline):
model = Client.deal.through
class ClientAdmin(admin.ModelAdmin):
inlines = [DealInline]
However, if you add a Client to a Deal and then open the Client detail page, the corresponding deal does not appear. Is there something I'm not connecting?
It is enough to have relation define only in one model. Otherwise you'll have 2 separate tables for separate ManyToMany relation: ClientDeal and DealClient.
What you need to do is to choose which one you need to leave. And probably update Admin inlines according to Django Admin documentation
class Client(models.Model):
deals = models.ManyToManyField('Deal', related_name="clients")
class Deal(models.Model):
pass
Yes, If you're using models.manytoMany() , you have to put it only in one model. no the two
But there's a very good attribute you should use: through
with through attribute you can create a intermediate model. here there's an example:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=200)
groups = models.ManyToManyField('Group', through='GroupMember', related_name='people')
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
class Group(models.Model):
name = models.CharField(max_length=200)
class Meta:
ordering = ['name']
def __unicode__(self):
return self.name
class GroupMember(models.Model):
person = models.ForeignKey(Person, related_name='membership')
group = models.ForeignKey(Group, related_name='membership')
type = models.CharField(max_length=100)
def __unicode__(self):
return "%s is in group %s (as %s)" % (self.person, self.group, self.type))
later, you can use your inline admin class!
I just tested this an you were actually really close.
First, #wowkin2 said, you don't want to define a ManyToManyField in both models so I would probably just define it in your Deal model.
Second, replace this:
class DealInline(admin.TabularInline):
model = Client.deal.through
with this:
class DealInline(admin.TabularInline):
model = Deal.client.through
And everything should work.
So, this is what your files should now look like:
models.py
class Deal(models.Model):
client = models.ManyToManyField(Client, related_name="deals")
admin.py
class ClientInline(admin.TabularInline):
model = Deal.client.through
class DealAdmin(admin.ModelAdmin):
inlines = [ClientInline]
class DealInline(admin.TabularInline):
model = Deal.client.through
class ClientAdmin(admin.ModelAdmin):
inlines = [DealInline]

Django admin: Edit fields of one-to-one model class

I have two models with the following relationship defined in models.py:
class InnerModel(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class OuterModel(models.Model):
inner = models.OneToOneField(InnerModel)
def __str__(self):
return "OuterModel"
My forms.py looks like this:
class OuterModelForm(forms.ModelForm)
class Meta:
model = OuterModel
fields = ['inner']
My admin.py form looks like this:
class OuterModelAdmin(admin.ModelAdmin)
form = OuterModelForm
admin.site.register(OuterModel, OuterModelAdmin)
When I display the admin page, I can see the InnerModel instance and the name field is present, but the name field is an empty drop-down menu rather than a blank text field that can be edited.
How can I change the InnerModel name field so that it can be edited by admin?
You need to use inlines (doc):
class InnerModelInline(admin.StackedInline):
model = InnerModel
class OuterModelAdmin(admin.ModelAdmin):
inlines = [InnerModelInline]
admin.site.register(OuterModel, OuterModelAdmin)
Similar question: here

Django admin: is there a way to know fields of the model?

My models are all based on a class called BaseModel like that:
class BaseModel(models.Model):
date_creation = models.DateTimeField(auto_now_add=True)
date_last_modif = models.DateTimeField(auto_now=True)
They all derive from it, for example:
class Langue(BaseModel):
nom = models.CharField(max_length=50)
I'd like to have fields date_creation and date_last_modif on one line like it's explained here. In the admin.py file, I'd like to have a "base" class where I define this thing. Something like:
class BaseAdmin(admin.ModelAdmin):
fields = (('date_creation', 'date_last_modif'),)
and then, for the Langue model in the admin.py file:
class LangueAdmin(BaseAdmin):
fields = ('name',)
Of course it's not working, I'm just looking for a solution to make it work.
How about doing something like this:
class LangueAdmin(BaseAdmin):
fields = BaseAdmin.fields + ('name',)

How to show subclass model in Django-admin inline form?

I got some models in models.py like this:
class QuestionPackage(models.Model):
"""Question Package"""
class Question(models.Model):
question_package = models.ManyToManyField(QuestionPackage, blank=True)
name = models.CharField(max_length=128)
answer = models.TextField()
class Reading(Question):
"""Reading Question"""
class Writing(Question):
"""Writing Question"""
(There are some differences between reading and writing ,but I omit that.)
and in admin.py:
class ReadingInline(admin.TabularInline):
model = Reading.question_package.through
class WritingInline(admin.TabularInline):
model = Writing.question_package.through
class QuestionPackageAdmin(admin.ModelAdmin):
inlines = [
ReadingInline, WritingInline,
]
I found that in question package admin page's inline form, both Reading and Writing shows like Question, I can't tell one from another. and they have no edit button.
What should I do to let admin shows subclass model in this situation?
Then in this case the super class Question should be an abstract class:
class Question(models.Model):
question_package = models.ManyToManyField(QuestionPackage, blank=True)
name = models.CharField(max_length=128)
answer = models.TextField()
class Meta:
abstract = True
https://docs.djangoproject.com/en/dev/topics/db/models/#abstract-base-classes
More info here Django Admin Interface Does Not Use Subclass's __unicode__()

Categories

Resources