Django db one of 2 foreign keys doesn’t return an object

In MySQL I have 3 tables: Client, Room and ClientRoom,which points on two previous tables.

-- -----------------------------------------------------
-- Table `client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `client` (
  `id` INT NOT NULL,
  `name` VARCHAR(255) NULL,
  PRIMARY KEY (`id`)
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;


-- -----------------------------------------------------
-- Table `room`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `room` (
  `id` INT NOT NULL,
  `price` DECIMAL(18,2) NOT NULL,
  `apart_num` INT NOT NULL,
  `free` BOOL default TRUE,
   PRIMARY KEY (`id`)
  )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;


-- -----------------------------------------------------
-- Table `client-room`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `client_room` (
  `id` INT PRIMARY KEY AUTO_INCREMENT,
  `client_id` INT NOT NULL,
  `room_id` INT NOT NULL,       
  `date_in` DATETIME NOT NULL,
  `date_out` DATETIME NOT NULL,
INDEX `fk_client_room_idx` (`client_id` ASC),
CONSTRAINT `fk_client_room_id`
    FOREIGN KEY (`client_id`)
    REFERENCES `client` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
INDEX `fk_room_idx` (`room_id` ASC),
CONSTRAINT `fk_room_id`
    FOREIGN KEY (`room_id`)
    REFERENCES `room` (`id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION
  )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

Using python manage.py inspectdb i got

class Client(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=255, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'client'


class ClientRoom(models.Model):
    client = models.ForeignKey(Client, models.DO_NOTHING)
    room = models.ForeignKey('Room', models.DO_NOTHING)
    date_in = models.DateTimeField()
    date_out = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'client_room'

class Room(models.Model):
    id = models.IntegerField(primary_key=True)
    price = models.DecimalField(max_digits=18, decimal_places=2)
    apart_num = models.IntegerField()
    free = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'room'

The problem is that to create an object of ClientRoom I require id of Client and object of Room, not the 2 objects: one of Client and one of Room.

def clientRoom(request):#This works
    if request.method == "POST":
        clientRoom = ClientRoom()
        clientRoom.client_id = request.POST.get("client_id")
        room=Room()
        room.id = request.POST.get("room")
        clientRoom.room = room
...
return render(request, "clientRoom.html", {"clientRoom": clientRoom})

def clientRoom(request):#This isn't working
    if request.method == "POST":
        clientRoom = ClientRoom()
        client=Client()
        client.id=request.POST.get("client_id")
        clientRoom.client_id = client
        room=Room()
        room.id = request.POST.get("room")
        clientRoom.room = room
...
return render(request, "clientRoom.html", {"clientRoom": clientRoom})

This makes no sense to me, and this also halts futher progress. I’ve been making gui site(on HTML), which shows Room number, and should show Client name,not id.But I can’t get Client name because ClientRoom doesn’t consist Client object fully, compare to Room.

<table><!-- Here I got only Client id, cannot access name by client_id.name,because client_id is just a number, while room is an object of Room, and i can access it's id -->
        {% for record in clientsRooms %}
        <tr> 
            <td>{{ record.client_id }}</td> <td>{{ record.room.id }}</td>
            <td>{{ record.date_in }}</td> <td>{{ record.date_out }}</td> 
            
        </tr>
        {% endfor %}
    </table>

What should I change to make ClientRoom constructor require 2 objects(Client and Room, not just Room object and Client id)?

Answer

When assigning an object to a foreign key field, don’t assign it to the field ending in _id. That’s why your one example isn’t working. You’re doing clientRoom.client_id = client, but client is a Client object, not an ID.

All of the following methods of assignment are valid for your models:

client_room = ClientRoom()
client_room.client_id = some_client_id
client_room.room_id = some_room_id


client_room = ClientRoom()
client_room.client = some_client_object
client_room.room = some_room_object


client_room = ClientRoom(client=some_client_object, room=some_room_object)


client_room = ClientRoom(client_id=some_client_id, room_id=some_room_id)


# And you can assign one by ID and the other by object, as you've already discovered.