Get() a Model object from it's UUID

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Get() a Model object from it's UUID



AIM



As part of a weather-dependent alarm clock project, each alarm will contain three categories of info: 1) date/time, 2) weather conditionals, 3) location.



EDIT



Once the User completes the SetAlarmForm, I am trying to create an Alarm object based on the Form-input and timezone, city and country based on the User's IP address.


SetAlarmForm


Alarm


timezone


city


country



I am attempting to retrieve the information inputted by the User in the Form, and assign that information to an Alarm object (identifiable by a UUID reference).


Form


Alarm



As requested in a comment, the URL is https://jqlc3gdp.apps.lair.io/login/



CODE



Models.py


class User(models.Model):
""" Model representing each User """
username = models.CharField(max_length=10, unique=True)

class Alarm(models.Model):
""" Model representing each Alarm """
alarm_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
# in future, remove null=True. I included it for the time being to workaround an error.
username = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
timezone = models.CharField(max_length=30)
city = models.CharField(max_length=30)
country = models.CharField(max_length=30)
time = models.DateTimeField()
temp_max = models.FloatField(blank=True, null=True)
temp_min = models.FloatField(blank=True, null=True)



Views.py


class SetAlarmView(FormView):
""" FormView for User to create the Alarm """
<code omitted>

def form_valid(self, form):
form.save()
self.get_user_timezone()
return super().form_valid(form)

def get_user_timezone(self):
""" Function to get User's timezone, city and country from IP """
<code omitted>
alarm_object, created = Alarm.objects.get_or_create(
alarm_id=uuid.uuid4(),
username=self.request.POST.get('username'),
timezone=timezone,
city=location.raw['address']['city'],
country=location.raw['address']['country'],
time=self.request.POST.get('time'),
temp_conditional=self.request.POST.get('temp_conditional'),
surf_conditional=self.request.POST.get('surf_conditional'),
temp_max=self.request.POST.get('temp_max'),
temp_min=self.request.POST.get('temp_min'),
surf_max=self.request.POST.get('surf_max'),
surf_min=self.request.POST.get('surf_min'),
)
alarm_object.save()





There are some strange things going on here. Alarm.objects.get would return a single item, not a tuple; did you mean to use get_or_create()? Why are you querying with an ID but by the time field? And why are you creating another separate Alarm object for the location?
– Daniel Roseman
14 hours ago




Alarm.objects.get


get_or_create()


time





can you please post the url.
– v Sugumar
14 hours ago





@DanielRoseman a reason for a couple of the issues you mentioned is because I copied a chunk of the code from a previous project, which for instance, contained an M2M relationship and hence the reason for the separate Alarm object for the location. After some reading, I have updated my question to better reflect what I am trying to achieve here.
– ycrad
24 mins ago





@vSugumar I have included a link to the URL in the question
– ycrad
14 mins ago




1 Answer
1



Just set the DateTimeField time to be unique (time = models.DateTimeField(unique=True)) in your models. Any time 2 instances have the same time, the last one won't get saved and an error will be thrown.


time = models.DateTimeField(unique=True)



If an alarm_id (the local variable, not the model field) (This is not a good name for it. It's not an id, it's a time representation that you're using as an identifier which also happens to clash with the name of an actual field in your model), is used that is used in another object, you can catch the error, alert the user and obviously not save the object.



Then, you can make queries like you are in your code.





Yes, I agree that would probably solve the problem described. Although, as an alarm clock, I don't want each time to be unique, but that's my mistake for not clarifying the question properly. In light of your answer and Daniel's comment, I have edited the question above to better reflect what I'm trying to achieve.
– ycrad
11 mins ago


time






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Better method to check all objects' parameters with a single call