Note that there are some explanatory texts on larger screens.

plurals
  1. POtastypie display raw foreign key id value
    primarykey
    data
    text
    <p>I'm doing my best to return values of related record ids (foreign key reference) using tastypie. By default, foreign keys are filtered out from the result.</p> <p>I've got a following model:</p> <pre><code>class Category(models.Model): """Finance category""" class Meta: db_table = 'category' parent = models.ForeignKey('self') name = models.CharField(max_length=32) TYPES = ( ('outcome', 'outcome'), ('income', 'income'), ) type = models.CharField(max_length=7,choices=TYPES) created_at = models.DateTimeField() updated_at = models.DateTimeField() created_by = models.ForeignKey(User, db_column='created_by', related_name='createdCategories') updated_by = models.ForeignKey(User, db_column='updated_by', related_name='updatedCategories') </code></pre> <p>I've got two relations here. <code>parent</code> is recursive relation (it's a category tree table). <code>created_by</code> is a relation to the user. API is returning following values: * id * name * created_at * updated_at * type * resource_uri</p> <p>What can I do to make tastypie return parent(_id) or created_by (or just any foreign key)?</p> <p>The following is what I've tried from another OS question:</p> <pre><code>class IncomeCategoryResource(ModelResource): parent_id = models.IntegerField(attribute="parent_id") class Meta: queryset = Category.objects.filter(type='income') </code></pre> <p>Unfortunately, the whole API fails:</p> <pre><code>__init__() got an unexpected keyword argument 'attribute' </code></pre> <p>I've also tried to replace <code>attribute</code> kwarg with <code>db_column</code>. This one is just ignored.</p> <p>Help me, please :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload