DRF: validate a field from another table

Pratibha

I have created an API to upload the file. Now I want to add few checks before user can upload it. So in payload I am asking his email and token to validate him.

Now email and token are in separate table. How can I validate them. I am getting errors like

TypeError: 'email' is an invalid keyword argument for this function

my models file

class File(models.Model):

    filename = models.FileField(blank=False, null=False,upload_to='files')
    remark = models.CharField(max_length=20)
    timestamp = models.DateTimeField(auto_now_add=True)

my serializer file

class FileSerializer(serializers.ModelSerializer):

    token = serializers.CharField(label=_("Token"))
    email = serializers.CharField(label=_('email'))

    def validate(self, attrs):
        print("validating params")
        token = attrs.get('token')
        email= attrs.get('email')

        validate(token, email)
        return attrs

    class Meta():
        model = File
        fields = ('filename', 'remark', 'timestamp', 'token', 'email')
        read_only_fields = ('token', 'email')
Ehsan Nouri

you only want email and token when creating a file(uploading) and also they are not fields in your model, so you should make them write_only and also you should override the create method in serializers and pop them out before saving into the model.

 class FileSerializer(serializers.ModelSerializer):

    token = serializers.CharField(label=_("Token"), write_only=True)
    email = serializers.CharField(label=_('router_macid'), write_only=True)

    def validate(self, attrs):
        print("validating params")
        token = attrs.get('token')
        email= attrs.get('email')

        validate(token, email)
        return attrs

    def create(self, validated_data):
        validated_data.pop('email', None)
        validated_data.pop('token', None)
        return super().create(validated_data)



    class Meta():
        model = File
        fields = ('filename', 'remark', 'timestamp', 'token', 'email')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding a field from another table to results

From Dev

How to create new field in view with some offset (field of table) from existing field (another field of table)?

From Dev

Copy Field contents from one Table to another table - FileMaker

From Dev

Django validate field based on value from another field

From Dev

SQLite, select where field 'like' field from another table

From Dev

Pull field from another table in MVC

From Dev

Pulling a field from another table in Hibernate

From Dev

Field involving multiple rows from another table

From Dev

Refresh a field from another table [Django]

From Dev

MySQL List some field from a single table and COUNT all particular field reference from another table

From Dev

Validate fields from one table to another in MySQL

From Dev

Set field to random value from another table

From Dev

Get value of another field in Field level Validation in DRF

From Dev

validate if another field greater than

From Dev

Can hibernate validator be used to validate a field depending on a field value from another class

From Dev

MYSQL Copy field from one table to another table

From Dev

Cakephp validate from another table when adding to a table

From Dev

Using values from a field in one table as column name for field in another

From Dev

Adding a field from another table to results

From Dev

select field from table in another field from another table

From Dev

Pull field from another table in MVC

From Dev

sql - show count of field from another table

From Dev

Select Additional Field from another Table

From Dev

Using a composite key from one table in a field in another table

From Dev

Add field in a Gii form from another table

From Dev

Delete trigger and getting field from another table

From Dev

update table with field boolean requirement from another table

From Dev

Bootstrap x-editable: Validate field against value from another

From Dev

Call field from another table in select firebase

Related Related

  1. 1

    Adding a field from another table to results

  2. 2

    How to create new field in view with some offset (field of table) from existing field (another field of table)?

  3. 3

    Copy Field contents from one Table to another table - FileMaker

  4. 4

    Django validate field based on value from another field

  5. 5

    SQLite, select where field 'like' field from another table

  6. 6

    Pull field from another table in MVC

  7. 7

    Pulling a field from another table in Hibernate

  8. 8

    Field involving multiple rows from another table

  9. 9

    Refresh a field from another table [Django]

  10. 10

    MySQL List some field from a single table and COUNT all particular field reference from another table

  11. 11

    Validate fields from one table to another in MySQL

  12. 12

    Set field to random value from another table

  13. 13

    Get value of another field in Field level Validation in DRF

  14. 14

    validate if another field greater than

  15. 15

    Can hibernate validator be used to validate a field depending on a field value from another class

  16. 16

    MYSQL Copy field from one table to another table

  17. 17

    Cakephp validate from another table when adding to a table

  18. 18

    Using values from a field in one table as column name for field in another

  19. 19

    Adding a field from another table to results

  20. 20

    select field from table in another field from another table

  21. 21

    Pull field from another table in MVC

  22. 22

    sql - show count of field from another table

  23. 23

    Select Additional Field from another Table

  24. 24

    Using a composite key from one table in a field in another table

  25. 25

    Add field in a Gii form from another table

  26. 26

    Delete trigger and getting field from another table

  27. 27

    update table with field boolean requirement from another table

  28. 28

    Bootstrap x-editable: Validate field against value from another

  29. 29

    Call field from another table in select firebase

HotTag

Archive