oscarmlage / django-cruds-adminlte

django-cruds is simple drop-in django app that creates CRUD for faster prototyping
BSD 3-Clause "New" or "Revised" License
424 stars 82 forks source link

Support fields from related models in CRUD views #78

Closed mbourqui closed 6 years ago

mbourqui commented 6 years ago

Hi,

This PR adds the possiblity to have fields from related models in CRUD views. For example, you can then have

from cruds_adminlte.crud import CRUDView

class MyView(CRUDView):
    from .models import MyModel
    model = MyModel
    list_fields =  ['my_related_field__related_model_field',]

where for example

from django.db import models

class MyOtherModel(models.Model):
    related_model_field = models.CharField(max_length=...)

class MyModel(models.Model):
    my_related_field = models.OneToOneField(MyOtherModel, on_delete=...)

where the __ (double underscore) allows you to look for a field related_model_field from a related model via my_related_field.

oscarmlage commented 6 years ago

Hey @mbourqui, I'm going to merge this but there are a couple of things I'll fix afterwards:

And as I've seen it's working only in the list template, not in the edit either the detail. Do you think it's possible to extend it to the other templates? Thanks in advance and thank you so much for your work.

mbourqui commented 6 years ago

Hey @oscarmlage, Thanks for merging my PR! Sorry for the compatibility and pep8 issues. I'm currently on a short deadline and did not had the time to fully check outside my use case. You are correct, it works for lists. I have yet only the use in list views, but give me three weeks and I'll make it work for other views too.

oscarmlage commented 6 years ago

Sure @mbourqui, don't worry at all!! we are not in a hurry or something similar :). The pep8 problems are already fixed. Please tell me if I can help you in anything. Thanks for your time and for contributing to this project too!.

crypto-vault commented 6 years ago

There's a side effect of this now, if you have columns with the same name on two objects you can't display the real objects name.

For example:

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    name = models.CharField(max_length=100)

class BookCRUD(CRUDView):
    model = Book
    fields = ['name', 'author']
    list_fields = ['name', 'author__name']

You get as a result list:

Name | Name Book Name | Author Name

Before this implementation you would get:

Name | Author Book Name | Author object (1)

When what would be preferred was: Name | Author Book Name | Author Name

The workaround for now is to rename column Author.name to Author.author.

It would be nice if we could display Name as Author without renaming columns in the database.