SpamExperts/pyzor

time data '2016-03-23T15:23' does not match format '%Y-%m-%d %H:%M'

kashyapk1 opened this issue · 0 comments

SERIALIZER.PY

from rest_framework import serializers
from sponsorapp.models import Sponsormodel
from datetime import datetime
import datetime as dtime
from dateutil.parser import parse
import pdb
from rest_framework.serializers import ValidationError 


class SponsorSerializer(serializers.ModelSerializer):

    offer_date = serializers.DateTimeField(default =None)
    sponsorship_date = serializers.DateTimeField(default =None)


    class Meta:
        model = Sponsormodel
        fields = ('sponsorship_date' , 'offername' , 'offer_date')
        #   pdb.set_trace()

    def validate_offername(self , value):
        offername = value
        if(len(offername) > 9):
            raise ValidationError("Please enter number of characters less than 9 or 9 ")

        return value    


    def validate_sponsorship_date(self , value):

        data = self.get_initial()
        offerdate = data.get('offer_date')


        offerdate = dtime.datetime.strptime(offerdate,'%Y-%m-%d %H:%M').strftime('%Y-%m-%d %H:%M')
        sponsorshipdate = value


        if(sponsorshipdate > offerdate):
            raise ValidationError("Check the dates")

        return value







MODELS.PY



from django.db import models



class Sponsormodel(models.Model):
    sponsorship_date = models.DateTimeField(default =None)
    offername = models.CharField(max_length = 100)
    offer_date = models.DateTimeField(default = None)

    def __str__(self):
        return self.offername