def get_serializable_data_for_fields ( model ) : pk_field = model . _meta . pk # If model is a child via multitable inheritance, use parent's pk while pk_field . remote_field and pk_field . remote_field . parent_link : pk_field = pk_field . remote_field . model . _meta . pk obj = { 'pk' : get_field_value ( pk_field , model ) } for field in model . _meta . fields : if field . serialize : obj [ field . name ] = get_field_value ( field , model ) return obj
python serializers django foreign keys
Return a serialised version of the model s fields which exist as local database columns ( i . e . excluding m2m and incoming foreign key relations )
def get_serializable_data_for_fields(model):
    """
    Return a serialised version of the model's fields which exist as local database
    columns (i.e. excluding m2m and incoming foreign key relations)
    """
    pk_field = model._meta.pk
    # If model is a child via multitable inheritance, use parent's pk
    while pk_field.remote_field and pk_field.remote_field.parent_link:
        pk_field = pk_field.remote_field.model._meta.pk

    obj = {'pk': get_field_value(pk_field, model)}

    for field in model._meta.fields:
        if field.serialize:
            obj[field.name] = get_field_value(field, model)

    return obj