how to make it so that if you change the value from true to false or vice versa in one place, all others change. class Ralevant default is True class Rooms default is True class Registration value changes and if in the registration class you change from true to false, then the Room class and Ralevant should also change
from django.contrib.auth.models import User
import datetime
class Ralevant(models.Model):
bool_roo = models.BooleanField(default=True)
def __str__(self):
return f'{self.bool_roo}'
year=datetime.datetime.now().year
month=datetime.datetime.now().month
day = datetime.datetime.now().day
class Rooms(models.Model):
room_num = models.IntegerField()
room_bool = models.ForeignKey(Ralevant, on_delete=models.CASCADE,
related_name='name1')
category = models.CharField(max_length=150)
def __str__(self):
return f'{self.room_num}'
class Registration(models.Model):
rooms = models.ForeignKey(Rooms, on_delete=models.CASCADE)
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
admin = models.ForeignKey(User, on_delete=models.CASCADE)
pasport_serial_num = models.CharField(max_length=100)
birth_date = models.DateField()
img = models.FileField()
visit_date = models.DateTimeField()
leave_date = models.DateTimeField(default=datetime.datetime(year=year,month=month,day=day 1,hour=12,minute=00,second=00))
guest_count = models.IntegerField()
room_bool = models.ForeignKey(Ralevant, on_delete=models.CASCADE,
related_name='name2')
def __str__(self):
return self.rooms,self.last_name,self.first_name
CodePudding user response:
You can implement it in your views.py
or in your serializers.py
, simple import model and update Model.objects.filter(pk=obj.pk).update()
something like that
CodePudding user response:
def obj(request, pk,):
reg = Registration.objects.get(pk=pk)
room = Rooms.objects.get(pk=pk)
if reg.room_bool == True:
room.room_bool = True
room.save()
if reg.room_bool == False:
room.room_bool = False
room.save()