---
# System prepended metadata

title: 'OKRE: Technical Standard'

---

# OKRE: Technical Standard

# Django: Serializer
* [How To Use DRF Serializers Effectively in Django](https://medium.com/better-programming/how-to-use-drf-serializers-effectively-dc58edc73998)
* [How to use DRF serializers effectively during write operations](https://medium.com/@raaj.akshar/how-to-effectively-use-django-rest-framework-serializers-during-write-operations-dd73b62c26b5)

📝 **Easy serializer of AbstractSoftDeletionModelController**
```
class Product(AbstractSoftDeletionModelController):
    name = models.CharField(max_length=255, db_index=True, null=True, blank=True)
    code = models.CharField(max_length=255, db_index=True, null=True, blank=True)
    length = models.DecimalField(max_digits=11, decimal_places=3, default=0, verbose_name='Length')
   
    class Meta:
        verbose_name_plural = 'Products'

    def __str__(self):
        return f'{self.code}: {self.name}'
```
```
class ProductSerializer(ModelControllerSerializer):
    id = serializers.IntegerField(required=False, allow_null=True)

    class Meta:
        model = Product
        exclude = EXCLUDE_COMMON_FIELDS
```

📝 **To include the parent relation you only need to include its serializer and include it in the fields list.**

```
class WithdrawalReturn(AbstractSoftDeletionModelController):
    producing_date = models.DateField(verbose_name='Producing Date')

    class Meta:
        verbose_name_plural = 'Withdrawal Return'

    def __str__(self):
        return f'{self.id}: {self.producing_date}'
 ```
 ```
class WithdrawalReturnItem(AbstractSoftDeletionModelController):
    withdrawal_return = models.ForeignKey(
        to='withdrawal_return.WithdrawalReturn',
        on_delete=models.CASCADE,
        db_index=True,
        related_name='items',
        verbose_name='Withdraw Return'
    )
    withdraw_amount = models.DecimalField(
        max_digits=10,
        decimal_places=3,
        default=0,
        verbose_name='Withdraw Amount',
    )

    class Meta:
        verbose_name_plural = 'Withdrawal Return Items'
```

```
class WithdrawalReturnItemSerializer(ModelControllerSerializer):
    id = serializers.IntegerField(required=False, allow_null=True)

    class Meta:
        model = WithdrawalReturnItem
        exclude = EXCLUDE_COMMON_FIELDS
        extra_kwargs = {
            'withdrawal_return': {'read_only': True}
        }


class WithdrawalReturnSerializer(ModelControllerSerializer):
    items = WithdrawalReturnItemSerializer(many=True)

    class Meta:
        model = WithdrawalReturn
        exclude = EXCLUDE_COMMON_FIELDS
```
📝 **ModelSerializer using model property (this work with version 3.8.2)**
```
class MyModelSerializer(serializers.ModelSerializer):
    ext_link = serializers.ReadOnlyField()

    class Meta:
        model = MyModel
        fields = "__all__"
```
Other read: https://www.django-rest-framework.org/api-guide/serializers/

# Django: Filter
Busy wait me gon 😭😭😭

# Export/Import excel
🔗 Import excel use django-excel-tools (by Maintainers: khemanorak) is Good!
https://pypi.org/project/django-excel-tools/
🔗 Export excel use Openpyxl
https://openpyxl.readthedocs.io/en/stable/

# Pdf generate
🔗 django-weasyprint
https://www.bedjango.com/blog/how-generate-pdf-django-weasyprint/

# Optimize query
Busy wait me gon 😭😭😭
