I have a post extension loaded in my django/django-cms/djangocms-blog project, that adds a few fields.
How can I get those fields from a Post object?
Here's an example of post:
$ python3 manage.py shell
>>> from djangocms_blog.models import Post
>>> post = Post.objects.first()
>>> post
<Post: Test post>
I just found that there's an extension
"reverse many to one" available in the Post
object:
>>> dir(post) # vvvvvvvvv I found this
['DoesNotExist', 'Meta', '[... truncated ...]', 'extension', '[... truncated ...]']
>>> post.extension
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0xd34db33f>
>>> post.extension.first()
<PostExtension: PostExtension object (1)>
So I just need to call it directly using post.extension.first()
:
>>> post.extension.first().my_custom_field
1
I don't think my Posts will have multiple post_extensions in this project so I'll stick with .first()
, but I think each app that adds a post_extension to the Posts will add another post_extension object to the object.
Edit: I can add others instances of the same post extension field to the Post object, I didn't know that post extension worked this way. I don't know how to limit the number of post extension per Post, though.
Second edit: I just found this function in the Django Admin Inline code:
def get_max_num(self, request, obj=None, **kwargs):
"""Hook for customizing the max number of extra inline forms."""
return self.max_num
So I added my own function in my PostExtensionInline(admin.TabularInline)
class:
def get_max_num(self, request, obj=None, **kwargs):
return 1