following is my code snippet:
class Captain(models.Model):
name = models.OneToOneField(User, on_delete=models.CASCADE)
date_of_birth = models.DateField(null=True, blank=True)
joined_date = models.DateField(null=True, blank=True)
class Boat(models.Model):
name = models.CharField(max_length=200)
contact = models.CharField(max_length=200)
captain = models.OneToOneField(Captain,on_delete=models.CASCADE, null=True, blank=True)
First there is a captain
instance created, lets say name=‘John’.
Then there are 3 boat
instances, named, “Boat A”, “Boat B” and “Boat C”,
My question is, is there a way “John” can query all the boat
instances(specifically the name
fields), so he could update, which boat he is on, at any given moment?
I have tried several ways to achieve this, but could only get the instances associated with the captain only, not the whole list of boats, (Boat A, Boat B, Boat C)
what is the best way to approach this problem without altering the models, the problem can be solved by altering the models, however when I alter the models I, have to fix a lot of templates work.
Hey there!
If you want to get all boats, that is:
Boat.objects.all()
But, if for example you want to get all boats that does not have a captain in the moment:
Boat.objects.filter(captain__isnull=True)
But, if for example, you want to get all boats that match a specific name:
boat_names = ["Abeerden 13", "The locust"]
Boat.objects.filter(name__in=boat_names)
This is all common operations, if you’re struggling with database queries using the ORM, i suggest that you review the section Making queries in the docs
Thanks, yes with the query Boat.objects.all()
I do get all the instances of the Boat model,
However I cannot seems to update the boat instance through the captain
instance.
Lets say, in early morning captain
instance ‘John’ was in in Boat
instance “Boat A”, and in the afternoon if he wanted to be in “Boat B” I could not make it update, followin is my forms.py and views.py
#forms.py
class BoatNameChange(forms.ModelForm):
boat_name = forms.ModelChoiceField(queryset=Boat.objects.all())
class Meta:
model = Boat
fields = ['boat_name']
#views.py
class BoatPickView(LoginRequiredMixin, generic.UpdateView):
model = Boat
form_class = BoatNameChange
template_name = 'captain/boat_change.html'
I hope I make myslef clear.
If you have an instance of Captain
named captain
, then because of the OneToOne relationship between Captain
and Boat
, the Boat
that captain
is related to is captain.boat
. (See the docs at Model field reference | Django documentation | Django) There is no need for a query at this point.
I believe (but have not verified this) that you should be able to reassign captain
from boat_a
to boat_b
by:
captain.boat.captain = None
boat_b.captain = captain
(It’s also possible that the first statement may also work as captain.boat = None
. Again, conjecture on my part. You may also need to save captain.boat
after the first statement and boat_b
after the second.)
Thanks will look in to it.