4

I have been tasked with creating Django Models for a hypothetical apartment booking application.

My question is: can I use a model that I've defined, as a field in another model?

For example, I will have one model called "Listing" that represents an apartment being listed.

class Listing(models.Model):
    address = models.IntegerField()
    owner = models.CharField(max_length=256)
    duration =  models.DurationField()
    price= models.IntegerField()

I also want to have a "Booking" model that represents an apartment once someone has booked it. It will have the exact same info as a Listing, with the addition of the username of the person who booked it. So can I have my Booking model use Listing as a field? And then just have one extra field for the booker's username.

Any other tips/critiques are highly appreciated as I am a complete beginner at Django.

2
  • Can a listing be booked by more than one person? You don't need a new field, you need a relationship Commented Nov 1, 2017 at 23:08
  • No, only by one person. So in my Booking model I would have something like " listing = models.OneToOneField(Listing, on_delete, etc...)? Commented Nov 1, 2017 at 23:16

2 Answers 2

8

I'm not 100% sure what you mean by use Listing as a field

But to me, you should be looking at the different built-in model relationships that exist in Django.

In your particular case, you will probably want to use a One-to-One relationship like so,

class Listing(models.Model):
    address = models.IntegerField()
    owner = models.CharField(max_length=256)
    duration =  models.DurationField()
    price= models.IntegerField()

class Booking(models.Model):
    listing= models.OneToOneField(
    Listing,
    on_delete=models.CASCADE,
    )
    username = models.Charfield()

Now if a user can book more than one apartment at a time, you'll be interested in a ForeignKey relationship like so,

class Listing(models.Model):
    address = models.IntegerField()
    owner = models.CharField(max_length=256)
    duration =  models.DurationField()
    price= models.IntegerField()

class Booking(models.Model):
    listing= models.ForeignKey(
    Listing,
    on_delete=models.CASCADE,
    )
    username = models.Charfield()

Note that in both examples I used Charfield for the username, feel free to use whatever Django field you need.

Sign up to request clarification or add additional context in comments.

Comments

3

The concept of a model as field is odd. What you can do is establish relationships between models, or to inherit one from the other. Given your situation, you can maybe inherit Booking from Listing:

The docs on this topic.

You'll have something like this:

class Listing(models.Model):
    address = models.IntegerField()
    owner = models.CharField(max_length=256)
    duration =  models.DurationField()
    price= models.IntegerField()

class Booking(Listing):
    #your new fields

1 Comment

Thank you so much! Inheritance is what I was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.