0

I have a model called 'Candidate', which has the fields Experience,New Experience,Date_posted.

I'm using CreateView form to store the values in the database. When I enter experience and date_posted(set to timezone.now) for the first time, the 'new experience' value should also be set to 'experience' value by default. Now this 'new experience' value should get incremented after every month.

For example, experience=2.4 ( 2 years 4 months ), new experience =2.4( set automatically )

So, If I open my template(website page) 1 month from now, the 'experience' and 'date_posted' values must be same, but 'new experience' = 2.5 ( 2 years, 5 months )

class CandidateCreateView(LoginRequiredMixin, CreateView):
   model = Candidate
   fields = ['candidate_name', 'experience', 'email', 'new_experience']

   def form_valid(self, form):
        form.instance.author = self.request.user
        return super().form_valid(form)

Also, I want to use the format, 2.12 ( for 2 years 12 months )and 3.1 ( for 2 years 13 months ). How do I achieve this?

1 Answers1

1

Maybe you can set your new_experience as a computed property, rather than a normal field?

from datetime import datetime

class Candidate:
    # ...
    experience = models.DateField()

    @property
    def new_experience(self):
        now = datetime.now().date()
        delta = now - self.date_posted
        return (self.experience + delta)

 Update

After your clarification in the comments, my suggestion would be to split the experience field into years and months:

class Candidate:
    # ...
    experience_years = models.IntegerField()
    experience_months = models.IntegerField()



    @property
    def new_experience(self):
        base_experience = datetime.datetime(year=self.expreience_years, month=self.experience_months, day=1)
        delta = datetime.datetime.now() - self.date_posted
        new_experience = base_experience + delta
        # now you can access new_experience.year, new_experience.month
        return new_experience

Another option be can be to return a tuple from new_experience (years,months), as computed above

Warning: Quick and dirty but not tested

Community
  • 1
  • 1
motatoes
  • 828
  • 11
  • 26
  • Oh, but will this give me the values in the format 2.1 , 2.2 etc? Like, if I make experience as a date field then it will have a specific value in the date format ( dd/mm/yy ) , if I subtract it from current date, I will get a date format, adding it back again will give me the current date only, won't it? – Aman Bhatnagar Aug 19 '19 at 08:06
  • You are right :) I should have used `date_posted` rather than experience to subtract it, please see updated answer – motatoes Aug 19 '19 at 08:19
  • You could then use a [custom tag filter](https://stackoverflow.com/a/46928226/1765562) to display it as you wish – motatoes Aug 19 '19 at 08:20
  • So should 'experience' still be a date field? I kept it as a CharField before because it just had to store values like 2.1,2.2 etc.. – Aman Bhatnagar Aug 19 '19 at 08:33
  • Basically I'm using this for a candidate looking for a job. So I need to store his details and keep a track. If on the date-posted his experience is 5 years, and if I'm viewing his profile after 5 months, his experience is actually 5.5 years now in his previous company. So that's why I took new_experience to store that. – Aman Bhatnagar Aug 19 '19 at 08:36
  • OK got it. My suggestion would be the following: if you want experience to be something like 2 years and 2 months etc. maybe you can split it to 2 integer fields like: `experience_years` and `experience_months`. In this case you can have the property add to this datetime object directly – motatoes Aug 19 '19 at 08:38
  • How do I print this new_experience in my template now? ```
    {{ form.new_experience|as_crispy_field }}
    ``` This is showing error.
    – Aman Bhatnagar Aug 19 '19 at 09:19
  • ```function missing required argument 'day' (pos 3)``` This error while submitting the form – Aman Bhatnagar Aug 19 '19 at 09:32
  • You don't need to include `new_experience` in your form, since it is computed. Just exclude it from your view fields. – motatoes Aug 19 '19 at 09:39
  • Ya I tried that. The form rendered. But on submitting showed some datetime error. I googled it and finally changed the header to just ```import datetime``` . But now the error changed to this ```function missing required argument 'day' (pos 3)``` – Aman Bhatnagar Aug 19 '19 at 09:48
  • I guess the datetime attribute requires a 'day' input to be passed. I tried adding an extra variable "day=0" to ```datetime.datetime(year=self.expreience_years, month=self.experience_months)``` . Because experience_day is not a field, and not needed as well for my case.But it threw some other error. – Aman Bhatnagar Aug 19 '19 at 09:51
  • I'm sorry i am not so familiar with datetime in python. Please try the updated code snippet in the answer. I hope that works – motatoes Aug 19 '19 at 10:29
  • That's okay, thanks a lot for the information. I'm getting that error only, trying to fix it. "day" issue. – Aman Bhatnagar Aug 20 '19 at 10:45
  • Did you try my updated snippet above ? I have addressed the ‘day’ issue in it – motatoes Aug 20 '19 at 10:46