-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added table_factory #532
Added table_factory #532
Conversation
Interesting idea! It might also be possible to replace (some of) the What will be an interesting detail is deciding on what kwargs we support. Ideally, not having to define every kwarg in the argument list of For reference: django Please also take some time to write documentation for this (at least a docstring). |
OK! Let's do this with
And I poke you |
@jieter Poke ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I've added some comments to your diff, please share your opinion if you do not agree.
The function also has to be added to https://github.com/jieter/django-tables2/blob/master/docs/pages/api-reference.rst in order to appear in the documentation.
@@ -1,5 +1,5 @@ | |||
# coding: utf-8 | |||
from .tables import Table, TableBase |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the string 'table_factory' should be added to __all__
, the # noqa
comment should be obsolete then.
django_tables2/tables.py
Outdated
@@ -620,3 +620,39 @@ class Table(TableBase): | |||
__doc__ = TableBase.__doc__ | |||
|
|||
# Table = DeclarativeColumnsMetaclass(str('Table'), (TableBase, ), {}) | |||
|
|||
|
|||
def table_factory(model, table=Table, **kwargs): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say, if **kwargs
doesn't allow passing arbitrary argument to the metaclass, it provides not much benefit to not explicitly define the arguments this function expects.
So either allow pass-through of arguments in some way, or be explicit about what arguments the function takes.
django_tables2/tables.py
Outdated
Meta = type(str('Meta'), parent, attrs) | ||
# Give this new form class a reasonable name. | ||
class_name = model.__name__ + str('Table') | ||
# Class attributes for the new form class. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems to be a leftover from copying modelform_factory?
django_tables2/views.py
Outdated
) | ||
if self.table_class is None: | ||
self.table_class = tables.table_factory(self.model) | ||
return self.table_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if this is not used with ListView
? I'd say if it is mixed in a class not defining self.model
, it should still raise the exception.
tests/test_views.py
Outdated
@@ -35,6 +35,10 @@ class SimpleView(DispatchHookMixin, tables.SingleTableView): | |||
model = Region # required for ListView | |||
|
|||
|
|||
class SimpleNoTableClassView(DispatchHookMixin, tables.SingleTableView): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't seem to use the functionality DispatchHookMixin
provides, if so, please remove.
OK NP ;) |
4c48767
to
53c3139
Compare
Hello @jieter
Yes, I think to declare Meta's attributes explicitly is the best idea. Maybe in future I'll declare Table's attributes too. |
@jieter Any news ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work! I've added another couple of comments.
django_tables2/views.py
Outdated
"{0}.table_class".format(klass) | ||
) | ||
self.table_class = tables.table_factory(self.model) | ||
return self.table_class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you undo the nesting here:
if self.table_class:
return self.table_class
if self.model:
return table_factory(self.model)
raise ImproperlyConfigured(...)
I think saving to self.table_class is not needed.
Also: the error message has table_class
twice, one must be model
.
docs/pages/generic-mixins.rst
Outdated
@@ -13,7 +13,8 @@ template. | |||
|
|||
The following view parameters are supported: | |||
|
|||
- ``table_class`` –- the table class to use, e.g. ``SimpleTable`` | |||
- ``table_class`` –- the table class to use, e.g. ``SimpleTable``, if not specfied | |||
a default table will be provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sentence needs to mention the default table will be based on self.model
and having a self.model
defined is required.
tests/test_models.py
Outdated
class Meta: | ||
fields = ('first_name',) | ||
|
||
tables.table_factory(Person, localize=localize) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assertions missing.
tests/test_views.py
Outdated
self.assertEqual(table_class, view.table_class) | ||
|
||
def test_get_tables_class_auto(self): | ||
view = SimpleNoTableClassView() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer having the definition of tables/views in the test as much as possible, so if only used in one test, please put SimpleNoTableClassView in this method.
Poke @jieter |
Merged, thanks. |
released 1.18.0 |
In the same idea of
django.forms.modelform_factory
, I created atable_factory
.It helps to create quickly
Table
s without declare a full class.I plan to use it in
django_tables2.views.SingleTableMixin
iftable_class
isn"t specified.Creating automaticaly a
Table
like done in django-filters or Django'sModelFormView
s.(I can do it in this PR ;) )