Django Inline : how to get inline properties in change_view() django










0














Email model class



 class Email(models.Model)
body_message = models.TextField(max_length = 256, null = False)
#other fields ForeignKey, subject, to etc.


Tabular Inline model.



#Inline Email
class EmailInline(admin.TabularInline):
model = Email


And this is my RequestAdmin model that shows inlines.



class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline,]

#In change_view() i want to print my inlines properties

def change_view(self, request, object_id, form_url='', extra_context=None):

for inline in self.inlines:
#Here i can remove my specific inline
self.inlines.remove(inline)
#But i want to check my inline_class properties
print(inline.body_message) #This line raise exception
#i want to print inlines body_message


return super(RequestAdmin, self).change_view(request, object_id)


Error image which i am getting.



Exception Value:

type object 'EmailInline' has no attribute 'body_message'



Now how i can get my inline properties (body_message) ?










share|improve this question





















  • But what is body_message supposed to be here? Is that a field on the Emali model?
    – Daniel Roseman
    Nov 12 at 7:21










  • @DanielRoseman yes body_message is a field of Email model. ( property of a model)
    – Faizan Fareed
    Nov 12 at 7:36










  • But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
    – Daniel Roseman
    Nov 12 at 8:43










  • @DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
    – Faizan Fareed
    Nov 12 at 10:49















0














Email model class



 class Email(models.Model)
body_message = models.TextField(max_length = 256, null = False)
#other fields ForeignKey, subject, to etc.


Tabular Inline model.



#Inline Email
class EmailInline(admin.TabularInline):
model = Email


And this is my RequestAdmin model that shows inlines.



class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline,]

#In change_view() i want to print my inlines properties

def change_view(self, request, object_id, form_url='', extra_context=None):

for inline in self.inlines:
#Here i can remove my specific inline
self.inlines.remove(inline)
#But i want to check my inline_class properties
print(inline.body_message) #This line raise exception
#i want to print inlines body_message


return super(RequestAdmin, self).change_view(request, object_id)


Error image which i am getting.



Exception Value:

type object 'EmailInline' has no attribute 'body_message'



Now how i can get my inline properties (body_message) ?










share|improve this question





















  • But what is body_message supposed to be here? Is that a field on the Emali model?
    – Daniel Roseman
    Nov 12 at 7:21










  • @DanielRoseman yes body_message is a field of Email model. ( property of a model)
    – Faizan Fareed
    Nov 12 at 7:36










  • But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
    – Daniel Roseman
    Nov 12 at 8:43










  • @DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
    – Faizan Fareed
    Nov 12 at 10:49













0












0








0







Email model class



 class Email(models.Model)
body_message = models.TextField(max_length = 256, null = False)
#other fields ForeignKey, subject, to etc.


Tabular Inline model.



#Inline Email
class EmailInline(admin.TabularInline):
model = Email


And this is my RequestAdmin model that shows inlines.



class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline,]

#In change_view() i want to print my inlines properties

def change_view(self, request, object_id, form_url='', extra_context=None):

for inline in self.inlines:
#Here i can remove my specific inline
self.inlines.remove(inline)
#But i want to check my inline_class properties
print(inline.body_message) #This line raise exception
#i want to print inlines body_message


return super(RequestAdmin, self).change_view(request, object_id)


Error image which i am getting.



Exception Value:

type object 'EmailInline' has no attribute 'body_message'



Now how i can get my inline properties (body_message) ?










share|improve this question













Email model class



 class Email(models.Model)
body_message = models.TextField(max_length = 256, null = False)
#other fields ForeignKey, subject, to etc.


Tabular Inline model.



#Inline Email
class EmailInline(admin.TabularInline):
model = Email


And this is my RequestAdmin model that shows inlines.



class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline,]

#In change_view() i want to print my inlines properties

def change_view(self, request, object_id, form_url='', extra_context=None):

for inline in self.inlines:
#Here i can remove my specific inline
self.inlines.remove(inline)
#But i want to check my inline_class properties
print(inline.body_message) #This line raise exception
#i want to print inlines body_message


return super(RequestAdmin, self).change_view(request, object_id)


Error image which i am getting.



Exception Value:

type object 'EmailInline' has no attribute 'body_message'



Now how i can get my inline properties (body_message) ?







django django-models django-admin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 6:57









Faizan Fareed

506




506











  • But what is body_message supposed to be here? Is that a field on the Emali model?
    – Daniel Roseman
    Nov 12 at 7:21










  • @DanielRoseman yes body_message is a field of Email model. ( property of a model)
    – Faizan Fareed
    Nov 12 at 7:36










  • But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
    – Daniel Roseman
    Nov 12 at 8:43










  • @DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
    – Faizan Fareed
    Nov 12 at 10:49
















  • But what is body_message supposed to be here? Is that a field on the Emali model?
    – Daniel Roseman
    Nov 12 at 7:21










  • @DanielRoseman yes body_message is a field of Email model. ( property of a model)
    – Faizan Fareed
    Nov 12 at 7:36










  • But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
    – Daniel Roseman
    Nov 12 at 8:43










  • @DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
    – Faizan Fareed
    Nov 12 at 10:49















But what is body_message supposed to be here? Is that a field on the Emali model?
– Daniel Roseman
Nov 12 at 7:21




But what is body_message supposed to be here? Is that a field on the Emali model?
– Daniel Roseman
Nov 12 at 7:21












@DanielRoseman yes body_message is a field of Email model. ( property of a model)
– Faizan Fareed
Nov 12 at 7:36




@DanielRoseman yes body_message is a field of Email model. ( property of a model)
– Faizan Fareed
Nov 12 at 7:36












But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
– Daniel Roseman
Nov 12 at 8:43




But inlines is a list of the InlineModelAdmin classes, it has nothing to do with the model instances.
– Daniel Roseman
Nov 12 at 8:43












@DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
– Faizan Fareed
Nov 12 at 10:49




@DanielRoseman alright. Any other way to work with model instance ? Like in save_model() method we can use formset() to modify the Inlines instance. I want to apply conditions on inline model instance properties.
– Faizan Fareed
Nov 12 at 10:49












1 Answer
1






active

oldest

votes


















1














I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:



from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
model = Email

# If you want to manipulate the query used to select the related objects:
def get_queryset(self, request):
queryset = super(EmailInline, self).get_queryset(request)
modified_queryset = queryset.exclude(body_message__contains='some string')
return modified_queryset


class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline, ]

# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets =
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)

# Custom code here - analyze the forms/objects, make changes to the form if desired:
if isinstance(inline, EmailInline):
for form in inline_admin_formset.forms:
if 'some string' in form.instance.body_message:
# this adds the "readonly" attribute to the input widget for the `body_message` field:
form.fields['body_message'].widget.attrs['readonly'] = True

inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets





share|improve this answer






















  • I also want to hide some inline using my inline details like using email.body_message ?
    – Faizan Fareed
    Nov 12 at 19:09










  • Hey there, just updated my answer.
    – YellowShark
    Nov 13 at 14:31










  • Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
    – Faizan Fareed
    Nov 13 at 19:20










  • Just updated my answer.
    – YellowShark
    Nov 13 at 20:30










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257212%2fdjango-inline-how-to-get-inline-properties-in-change-view-django%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:



from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
model = Email

# If you want to manipulate the query used to select the related objects:
def get_queryset(self, request):
queryset = super(EmailInline, self).get_queryset(request)
modified_queryset = queryset.exclude(body_message__contains='some string')
return modified_queryset


class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline, ]

# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets =
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)

# Custom code here - analyze the forms/objects, make changes to the form if desired:
if isinstance(inline, EmailInline):
for form in inline_admin_formset.forms:
if 'some string' in form.instance.body_message:
# this adds the "readonly" attribute to the input widget for the `body_message` field:
form.fields['body_message'].widget.attrs['readonly'] = True

inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets





share|improve this answer






















  • I also want to hide some inline using my inline details like using email.body_message ?
    – Faizan Fareed
    Nov 12 at 19:09










  • Hey there, just updated my answer.
    – YellowShark
    Nov 13 at 14:31










  • Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
    – Faizan Fareed
    Nov 13 at 19:20










  • Just updated my answer.
    – YellowShark
    Nov 13 at 20:30















1














I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:



from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
model = Email

# If you want to manipulate the query used to select the related objects:
def get_queryset(self, request):
queryset = super(EmailInline, self).get_queryset(request)
modified_queryset = queryset.exclude(body_message__contains='some string')
return modified_queryset


class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline, ]

# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets =
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)

# Custom code here - analyze the forms/objects, make changes to the form if desired:
if isinstance(inline, EmailInline):
for form in inline_admin_formset.forms:
if 'some string' in form.instance.body_message:
# this adds the "readonly" attribute to the input widget for the `body_message` field:
form.fields['body_message'].widget.attrs['readonly'] = True

inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets





share|improve this answer






















  • I also want to hide some inline using my inline details like using email.body_message ?
    – Faizan Fareed
    Nov 12 at 19:09










  • Hey there, just updated my answer.
    – YellowShark
    Nov 13 at 14:31










  • Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
    – Faizan Fareed
    Nov 13 at 19:20










  • Just updated my answer.
    – YellowShark
    Nov 13 at 20:30













1












1








1






I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:



from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
model = Email

# If you want to manipulate the query used to select the related objects:
def get_queryset(self, request):
queryset = super(EmailInline, self).get_queryset(request)
modified_queryset = queryset.exclude(body_message__contains='some string')
return modified_queryset


class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline, ]

# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets =
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)

# Custom code here - analyze the forms/objects, make changes to the form if desired:
if isinstance(inline, EmailInline):
for form in inline_admin_formset.forms:
if 'some string' in form.instance.body_message:
# this adds the "readonly" attribute to the input widget for the `body_message` field:
form.fields['body_message'].widget.attrs['readonly'] = True

inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets





share|improve this answer














I think I understand your question a bit better - you want to affect the actual objects that are being populated into the inline form(s), and hide certain ones based on the contents of the body_message property. In that case, you can override the InlineModelAdmin.get_queryset method, which will give you the opportunity to exclude certain items; or the ModelAdmin.get_inline_formsets method, which gives you the opportunity to alter the forms that will be generated for the inline objects:



from django.contrib.admin.options import TabularInline

class EmailInline(TabularInline):
model = Email

# If you want to manipulate the query used to select the related objects:
def get_queryset(self, request):
queryset = super(EmailInline, self).get_queryset(request)
modified_queryset = queryset.exclude(body_message__contains='some string')
return modified_queryset


class RequestAdmin(admin.ModelAdmin):
inlines = [EmailInline, ]

# If you wanted to manipulate the inline forms, to make one of the fields read-only:
def get_inline_formsets(self, request, formsets, inline_instances, obj=None):
inline_admin_formsets =
for inline, formset in zip(inline_instances, formsets):
fieldsets = list(inline.get_fieldsets(request, obj))
readonly = list(inline.get_readonly_fields(request, obj))
prepopulated = dict(inline.get_prepopulated_fields(request, obj))
inline_admin_formset = helpers.InlineAdminFormSet(
inline, formset, fieldsets, prepopulated, readonly,
model_admin=self,
)

# Custom code here - analyze the forms/objects, make changes to the form if desired:
if isinstance(inline, EmailInline):
for form in inline_admin_formset.forms:
if 'some string' in form.instance.body_message:
# this adds the "readonly" attribute to the input widget for the `body_message` field:
form.fields['body_message'].widget.attrs['readonly'] = True

inline_admin_formsets.append(inline_admin_formset)
return inline_admin_formsets






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 at 20:29

























answered Nov 12 at 14:40









YellowShark

1,024911




1,024911











  • I also want to hide some inline using my inline details like using email.body_message ?
    – Faizan Fareed
    Nov 12 at 19:09










  • Hey there, just updated my answer.
    – YellowShark
    Nov 13 at 14:31










  • Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
    – Faizan Fareed
    Nov 13 at 19:20










  • Just updated my answer.
    – YellowShark
    Nov 13 at 20:30
















  • I also want to hide some inline using my inline details like using email.body_message ?
    – Faizan Fareed
    Nov 12 at 19:09










  • Hey there, just updated my answer.
    – YellowShark
    Nov 13 at 14:31










  • Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
    – Faizan Fareed
    Nov 13 at 19:20










  • Just updated my answer.
    – YellowShark
    Nov 13 at 20:30















I also want to hide some inline using my inline details like using email.body_message ?
– Faizan Fareed
Nov 12 at 19:09




I also want to hide some inline using my inline details like using email.body_message ?
– Faizan Fareed
Nov 12 at 19:09












Hey there, just updated my answer.
– YellowShark
Nov 13 at 14:31




Hey there, just updated my answer.
– YellowShark
Nov 13 at 14:31












Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
– Faizan Fareed
Nov 13 at 19:20




Alright. But if i want to apply read_only_fields on specific fields of inline using myqueryset as you mentioned above filtering email.body_message. How i can achieve it ?
– Faizan Fareed
Nov 13 at 19:20












Just updated my answer.
– YellowShark
Nov 13 at 20:30




Just updated my answer.
– YellowShark
Nov 13 at 20:30

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257212%2fdjango-inline-how-to-get-inline-properties-in-change-view-django%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3