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

  • 1
    possible duplicate of : https://stackoverflow.com/questions/30911612/how-can-i-set-a-datefield-format-in-django-from-the-model – Chitrank Dixit Nov 14 '19 at 12:00
  • @ChitrankDixit my question isn't duplicated with question on link you attached. Read it more attentively – Наглый Спамер Nov 14 '19 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. – Chitrank Dixit Nov 14 '19 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 – Mehak Nov 14 '19 at 12:11
  • @Mehak i want to store in my database only year information not month and day – Наглый Спамер Nov 14 '19 at 12:13
  • @Mehak i was thinking so but if i want store it in dateField is it possible? – Наглый Спамер Nov 14 '19 at 12:25
  • @Mehak good idea but i will try positive integer field. Thank you man – Наглый Спамер Nov 14 '19 at 12:36
  • I added an answer to this and deleting the unnecessary comments – Mehak Nov 14 '19 at 12:48

1 Answers1

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

Mehak
  • 961
  • 9
  • 20