0

I have the following model

class Meeting(models.Model):
    meeting_title = models.CharField(default='', max_length=128, blank=True, null=True)
    meeting_time = models.TimeField(blank=False, null=False)
    meeting_date = models.DateField(blank=False, null=False)

    def meeting_datetime(self):
        return datetime.combine(self.meeting_date, self.meeting_time)

However, when I try to access the Meeting object via meeting_datetime I get Cannot resolve keyword 'meeting_datetime' into field. Choices are: id, meeting_title, meeting_time, meeting_date.

Why is meeting_datetime not an option? Doesn't the def create it as a DateTimeField? If not, how do I add it as a field, or otherwise access it?

alias51
  • 8,178
  • 22
  • 94
  • 166
  • 1
    meeting_datetime is just a model function, it is not treated as a field. Either you need to create a datetime field in your model or you have to compare meeting_time and meeting_date for expected answer. – Geo Jacob Aug 03 '15 at 13:50
  • @GeoJacob Thanks, so if I add `meeting_datetime = models.DateTimeField()` how can I dynamically add the field value to use `datetime.combine(self.meeting_date, self.meeting_time)`? Do I need to do this in the view when the form is submitted? – alias51 Aug 03 '15 at 13:55

2 Answers2

2

That's because functions aren't represented as fields in Django. You have to call it as a function to get its value (m.meeting_datetime()). However, if you really want to access the value without it looking like a function call, you can use the property decorator:

class Meeting(models.Model):
    meeting_title = models.CharField(default='', max_length=128, blank=True, null=True)
    meeting_time = models.TimeField(blank=False, null=False)
    meeting_date = models.DateField(blank=False, null=False)

    @property
    def meeting_datetime(self):
        return datetime.combine(self.meeting_date, self.meeting_time)

That will allow you to access the meeting_datetime function as if it were a property:

>>> m = Meeting(...)
>>> m.meeting_datetime
# data

That being said, is there a good reason you separated a DateField and TimeField and didn't just use a single DateTimeField?

class Meeting(models.Model):
    meeting_title = models.CharField(default='', max_length=128, blank=True, null=True)
    meeting_datetime = models.DateTimeField()

Then you'd be able to access that field the same as you would above with the decorator example. Except that the value would be stored in your database, while the decorator example's value would be derived only when you access the property.

JoeLinux
  • 4,198
  • 1
  • 29
  • 31
1

Either you can rewrite your model as:

class Meeting(models.Model):
   meeting_title = models.CharField(default='', max_length=128, blank=True, null=True)
   meeting_time = models.TimeField(blank=False, null=False)
   meeting_date = models.DateField(blank=False, null=False)
   meeting_datetime = models.DateTimeField(blank=True, null=False)

   def save(self, *args, **kwargs):
      self.meeting_datetime = datetime.combine(self.meeting_date, self.meeting_time)
      super(Meeting, self).save(*args, **kwargs)

or

you can filter your queryset as:

meeting_objs = Meeting.objects.filter(meeting_date__gt=datetime.date.today(),meeting_time__gt=datetime.datetime.now().time() )
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
  • How do I make `meeting_datetime` timezone aware? – alias51 Aug 03 '15 at 17:10
  • i think this link may help you http://stackoverflow.com/questions/22108634/django-how-to-make-a-datetime-object-aware-of-the-timezone-in-which-it-was-crea – Geo Jacob Aug 03 '15 at 17:40