0

I have a model Team

class Team(models.Model):                             
    team = models.IntegerField(primary_key=True)      
    name = models.CharField(max_length=170)         
    code = models.CharField(max_length=20)          
    logo = models.URLField(null=True)
    country_id = models.ForeignKey('Country',null=True, on_delete=models.SET_NULL)                     
    founded = models.DateField(null=True)
    venue_name = models.CharField(max_length=170)
    venue_surface = models.CharField(max_length=170)
    venue_address = models.CharField(max_length=200)                                                
    venue_city = models.CharField(max_length=150)                                                   
    venue_capacity = models.IntegerField(null=True)                                                 
    lastModified = models.DateTimeField(auto_now=True)

I want to create object from this model where input data for founded field is only year data for example 1970

How i can do it. Thanks in advance

8
  • 1
    possible duplicate of : stackoverflow.com/questions/30911612/… Commented Nov 14, 2019 at 12:00
  • @ChitrankDixit my question isn't duplicated with question on link you attached. Read it more attentively Commented Nov 14, 2019 at 12:04
  • At the subject of the question , you are asking how to specify the date field format while creating django objects, but in the description you ask about date format also and also the logic to for querying the data. No issues with that but if you ask such questions please specify what have you tried so far , with clear subject and relevant description, it would provider clear information to people and therefore people can answer you better. Commented Nov 14, 2019 at 12:09
  • Do you mean to say that you want the date field to store the date in these ways: only year/year with month/whole date? You will have to use separate fields if that's the case Commented Nov 14, 2019 at 12:11
  • @Mehak i want to store in my database only year information not month and day Commented Nov 14, 2019 at 12:13

1 Answer 1

1

If you want to store just the year for the founded field, you can try PositiveSmallIntegerField for that case.

class Team(models.Model):                             
    # Other Fields                   
    founded = models.PositiveSmallIntegerField(null=True)

You can add your own validators or use clean method of your model to validate whether the user has entered the correct year or not.

If you want to use a DateField only, then you will have to give a default value to Date and Month in that case, if the year is 1970, it can be stored like 01/01/1970 and the year can be accessed using obj.founded.year

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.