A model used for curating other models and proxying their attributes
Project description
# django-curation
**django-curation** is a django module that provides a model used for curating
other model objects and proxying their attributes.
## Example
```python
from django.db import models
from curation.models import CuratedItem, CuratedGroup, CuratedItemManager
from curation.fields import CuratedForeignKey
from blog.models import Post
class CuratedPostGroup(CuratedGroup):
pass
class CuratedPost(CuratedItem):
formfield_overrides = {
'custom_title': 'title',
}
objects = CuratedItemManager()
group = models.ForeignKey(CuratedPostGroup)
post = curation.fields.CuratedForeignKey(Post)
custom_title = models.CharField(max_length=255, null=True, blank=True,
db_column='title')
```
## Internals
### *class* **curation.models.CuratedItem**
Abstract class representing an item in a curated group.
In order for a model that extends this class to proxy successfully,
it must define a **CuratedForeignKey** field.
```python
class CuratedPost(CuratedItem):
post = curation.fields.CuratedForeignKey(Post)
```
##### field_overrides = *{}*
A dict that maps field names in the proxy model (the to=... model in the
**CuratedForeignKey**) to field names in the current model which can override
them (provided their value is not None or an empty string).
This takes the form:
```python
field_overrides = {
'title': 'custom_title',
'status': 'custom_status',
}
```
Where `custom_title` and `custom_status` are fields in the model extending
**CuratedItem**, and `title` and `status` are fields in the proxy model.
##### primary_id = *models.AutoField(primary_key=True, db_column='id')*
Custom primary key to prevent conflicts with the proxy model's primary key.
<hr/>
### *class* curation.models.CuratedItemManager
A manager that defines queryset helpers for CuratedItem.
##### group(*slug*)
Filter the current queryset to rows with curated groups having slug "slug".
<hr/>
### *class* **curation.base.CuratedItemModelBase**
Overrides **ModelBase** to check whether a **curation.fields.CuratedForeignKey**
is defined on the model. If not, throw a **TypeError**.
<hr/>
### *class* **curation.fields.CuratedForeignKey**
A ForeignKey that gets a list of the `__dict__` keys and field names of the
related model on load. It saves this list to the `_proxy_attrs` attribute of
its parent model's `_meta` attribute.
##### contribute_to_class(*cls, name*)
A django built-in that adds attributes to the model class in which it is
defined.
This method sets the `_curated_proxy_field_name` on the `_meta` attribute
of the **CuratedForeignKey**'s parent model to the field's name (e.g. "post"
in the example at the very beginning of this README).
##### contribute_to_related_class(*cls, related*)
A django built-in that adds attributes to the class a **RelatedField**
points to.
In this case we're adding `_proxy_attrs` to the _meta attribute of the
**ForeignKey**'s parent model, not the related model. The reason we're not
using `contribute_to_class` is that we need the related class to be
instantiated to obtain its field names, and the related class may not be loaded
yet when `contribute_to_class` is called (for instance, if it is lazy loaded,
when the ForeignKey field is defined using a string for the model).
**django-curation** is a django module that provides a model used for curating
other model objects and proxying their attributes.
## Example
```python
from django.db import models
from curation.models import CuratedItem, CuratedGroup, CuratedItemManager
from curation.fields import CuratedForeignKey
from blog.models import Post
class CuratedPostGroup(CuratedGroup):
pass
class CuratedPost(CuratedItem):
formfield_overrides = {
'custom_title': 'title',
}
objects = CuratedItemManager()
group = models.ForeignKey(CuratedPostGroup)
post = curation.fields.CuratedForeignKey(Post)
custom_title = models.CharField(max_length=255, null=True, blank=True,
db_column='title')
```
## Internals
### *class* **curation.models.CuratedItem**
Abstract class representing an item in a curated group.
In order for a model that extends this class to proxy successfully,
it must define a **CuratedForeignKey** field.
```python
class CuratedPost(CuratedItem):
post = curation.fields.CuratedForeignKey(Post)
```
##### field_overrides = *{}*
A dict that maps field names in the proxy model (the to=... model in the
**CuratedForeignKey**) to field names in the current model which can override
them (provided their value is not None or an empty string).
This takes the form:
```python
field_overrides = {
'title': 'custom_title',
'status': 'custom_status',
}
```
Where `custom_title` and `custom_status` are fields in the model extending
**CuratedItem**, and `title` and `status` are fields in the proxy model.
##### primary_id = *models.AutoField(primary_key=True, db_column='id')*
Custom primary key to prevent conflicts with the proxy model's primary key.
<hr/>
### *class* curation.models.CuratedItemManager
A manager that defines queryset helpers for CuratedItem.
##### group(*slug*)
Filter the current queryset to rows with curated groups having slug "slug".
<hr/>
### *class* **curation.base.CuratedItemModelBase**
Overrides **ModelBase** to check whether a **curation.fields.CuratedForeignKey**
is defined on the model. If not, throw a **TypeError**.
<hr/>
### *class* **curation.fields.CuratedForeignKey**
A ForeignKey that gets a list of the `__dict__` keys and field names of the
related model on load. It saves this list to the `_proxy_attrs` attribute of
its parent model's `_meta` attribute.
##### contribute_to_class(*cls, name*)
A django built-in that adds attributes to the model class in which it is
defined.
This method sets the `_curated_proxy_field_name` on the `_meta` attribute
of the **CuratedForeignKey**'s parent model to the field's name (e.g. "post"
in the example at the very beginning of this README).
##### contribute_to_related_class(*cls, related*)
A django built-in that adds attributes to the class a **RelatedField**
points to.
In this case we're adding `_proxy_attrs` to the _meta attribute of the
**ForeignKey**'s parent model, not the related model. The reason we're not
using `contribute_to_class` is that we need the related class to be
instantiated to obtain its field names, and the related class may not be loaded
yet when `contribute_to_class` is called (for instance, if it is lazy loaded,
when the ForeignKey field is defined using a string for the model).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
django-curation-1.0.31.tar.gz
(25.2 kB
view details)
Built Distribution
File details
Details for the file django-curation-1.0.31.tar.gz
.
File metadata
- Download URL: django-curation-1.0.31.tar.gz
- Upload date:
- Size: 25.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0dd31abc3b44d26661f2a34e95a5c3bc1d74925b760c641fac1cf4915e157d53 |
|
MD5 | 33e4d57fcda3060382713cf9204ba2a3 |
|
BLAKE2b-256 | d18fc65bc465a8c5ff9e6afc945aee1d9e16355ebda0457b6f6db879ac0515f0 |
File details
Details for the file django_curation-1.0.31-py2-none-any.whl
.
File metadata
- Download URL: django_curation-1.0.31-py2-none-any.whl
- Upload date:
- Size: 29.0 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7050f42cea896e1e39403be16b17ee6c0d88289564ec2dd6647c3fed4eae2255 |
|
MD5 | 1c4e36db6c2507f636f8261803a49ac9 |
|
BLAKE2b-256 | 44a47fc75d1502dd0cfaa2f4ea1e6489d1a640e8242d3acf3849ed49225024cf |