-
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to register fields to models
- Loading branch information
Jose Soares
committed
Jan 27, 2010
1 parent
7a0d437
commit ff92ac9
Showing
2 changed files
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import fields | ||
import models | ||
|
||
from django.db.models import FieldDoesNotExist | ||
|
||
def register_m2m(model, field_name, extra_params={}): | ||
return _register(model, field_name, extra_params, fields.CategoryM2MField) | ||
|
||
|
||
def register_fk(model, field_name, extra_params={}): | ||
return _register(model, field_name, extra_params, fields.CategoryFKField) | ||
|
||
def _register(model, field_name='category', extra_params={}, field=fields.CategoryFKField): | ||
opts = model._meta | ||
try: | ||
opts.get_field(field_name) | ||
except FieldDoesNotExist: | ||
field(**extra_params).contribute_to_class(model, field_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.db.models import ForeignKey, ManyToManyField | ||
from categories.models import Category | ||
|
||
class CategoryM2MField(ManyToManyField): | ||
def __init__(self, **kwargs): | ||
super(CategoryM2MField, self).__init__(to=Category, **kwargs) | ||
|
||
|
||
class CategoryFKField(ForeignKey): | ||
def __init__(self, **kwargs): | ||
super(CategoryFKField, self).__init__(to=Category, **kwargs) |