I am having a user select from a list of Pending shift swap requests. in the html page the form is as such
#pending_swaps.html
{% for pending in swappie_pending_list %}
<form action="{% url 'ShiftSwap:pending_swaps' %}" method="post">
{% csrf_token %}
<p>{{ pending.swapper.name }} wants to switch your {{ pending.get_swappie_day_display }} for their {{ pending.get_swapper_day_display }}
<input type="hidden" name="Swap_Obj" value="{{ pending }}">
<input type="submit" name="Confirm" value="Confirm">
<input type="submit" name="Deny" value="Deny">
</p>
</form>
{% endfor %}
in this code the swappie_pending_list is a list of Pending_Swap Model objects. However when I try to make a hidden input of value={{ pending }} it simply returns the str of the model instead of an instance to the model itself
here is the Pending_swap model
class Pending_Swap(models.Model):
'''
a model to house two users, and two days to keep track of which swaps are currently pending \n
swapper: user
swappie: who they want to swap with
swapper_day: day they are giving up
swappie_day: day the user wants to take
'''
Days_of_week = (
('SU', 'Sunday'),
('MO', 'Monday'),
('TU', 'Tuesday'),
('WE', 'Wednesday'),
('TH', 'Thursday'),
('FR', 'Friday'),
('SA', 'Saturday'),
)
swapper = models.ForeignKey(Person, on_delete=models.CASCADE, default=" ", related_name='Swapper')
swappie = models.ForeignKey(Person, on_delete=models.CASCADE, default=" ", related_name='Swappie')
swapper_day = models.CharField(max_length=2, choices=Days_of_week)
swappie_day = models.CharField(max_length=2, choices=Days_of_week)
confirm = models.BooleanField(default=False)
def __str__(self):
return "{0} and {1} pending swap {2} for {3}".format(self.swapper.name, self.swappie.name, self.swapper_day, self.swappie_day)
image of the stack to show the different variable
is there anyway to get the instance from the template instead of a string?
CodePudding user response:
It doesn't fix the problem, however, as a workaround, I was able to split the str into a working form to "re get" the object
swap_name = swap_obj.split()
swapper = Person.objects.get(name=swap_name[0])
swappie = Person.objects.get(name=swap_name[2])
swapper_day = swap_name[5]
swappie_day = swap_name[7]
swap_obj is the Swap_Obj (str from the model) from the template so the initial question was not answered but I was able to get around it.