question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to call fields from Revision model in a custom inherited model?

See original GitHub issue

For example, if I need call date_created from Revision in my admin.py as list_display field? Is there possible?

@admin.register(Container)
class ContainerAdmin(CompareVersionAdmin, admin.ModelAdmin):
    list_display = ('date_created', 'id_auto_series', 'id_human', 'uuid', 'title', 'description', 'description_level',)
    filter_horizontal = ('items', 'container_child')
 ERRORS:
django_1    | <class 'collection.admin.ContainerAdmin'>: (admin.E108) The value of 'list_display[0]' refers to 'date_created', which is not a callable, an attribute of 'ContainerAdmin', or an attribute or method on 'collection.Container'.

selecao_038

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
etianencommented, Jul 19, 2018

Oh, and another correction:

def get_date_created(self): return Version.objects.get_for_ object(self).order_by(“id”).first().revision.date_created

It would be a lot easier to just add a “date_created” field to your model directly, however.

On 19 July 2018 at 16:06, Dave Hall dave@etianen.com wrote:

When you register a model with django-reversion, it doesn’t extend the model. So the Revision fields will not be available on your model.

Your get_date_created method has a type. The second “version” should be lowercase, as below:

def get_date_created(self): Version.objects.get_for_object(self) return version.revision.date_created

On 19 July 2018 at 15:55, Photonauta 35 notifications@github.com wrote:

hi @etianen https://github.com/etianen, thanks for answer here. I’m not sure about the clarity of topic, so I’m sorry, let me ask again.

My question is: how can we call a property of Revision model after a extended model one (please, correct me if I has some misunderstanding).

  1. I have a model called Post in my model.py file. This model has reversion register to be extended from Revision model, right?

@reversion.register() class Post(models.Model): “”“Used to store posts”“” title = models.CharField( max_length=256) content = models.TextField( null=True, blank=True)

def __str__(self):
    return self.title

class Meta:
    verbose_name = 'Post'
    verbose_name_plural = 'Posts'
  1. So, in admin,py from the same model, I wanna call field in list_display to be listed, but field from Revision model seems dont be callables.

@admin.register(Post) class PostAdmin(CompareVersionAdmin, admin.ModelAdmin): list_display = (‘id’, ‘date_created’, ‘title’) search_fields = [‘all’]

Error message:

ERRORS: django_1 | <class ‘post.admin.PostAdmin’>: (admin.E108) The value of ‘list_display[1]’ refers to ‘date_created’, which is not a callable, an attribute of ‘PostAdmin’, or an attribute or method on ‘post.Post’.

So, despite of ‘date_created’ be a field from Revision and Revision be “extended” in Post, I dont just call ‘date_created’ in presets like list_display.

What I misunderstand here?

I tryed to create a function in my model, but again ‘date_created’ is not a valid field.

def get_date_created(self):
    return self.date_created

AttributeError: ‘Post’ object has no attribute ‘date_created’

This has no effect at all too:

def get_date_created(self):
    Version.objects.get_for_object(self)
    return Version.revision.date_created

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/etianen/django-reversion/issues/741#issuecomment-406305689, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJFCLm-h3ogVus2JmMA8T-CH_f-gd_0ks5uIJ35gaJpZM4VPUaf .

0reactions
intrepidocabralcommented, Jul 19, 2018

When you register a model with django-reversion, it doesn’t extend the model. So the Revision fields will not be available on your model.

ok! I got it now. Thank you.

def get_date_created(self): return Version.objects.get_for_object(self).order_by(“id”).first().revision.date_created

This works pretty well! Thank you again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

django-reversion API - Read the Docs
A revision represents one or more changes made to your model instances, grouped together as a single unit. You create a revision by...
Read more >
Django - save copy of instance in inherited model
I want to save 'revisions' of the model as the inherited model. All fields should be the same at the time of the...
Read more >
Sharing custom models
In this tutorial, we will show you how to write a custom model and its ... In both cases, notice how we inherit...
Read more >
List work item fields and attributes in Azure Boards
The isPicklist and isPicklistSuggested attributes are only assigned to custom fields defined for an inherited process. The Inherited process ...
Read more >
Inherited fields, modified model and views
partner (or not?). If I look at my model table in the database I can see all the fields (the ones from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found