doniyor
doniyor

Reputation: 37866

can I save models in this way in Django ?

first_obj = MyFirstModel()
second_obj = ImageModel()

first_obj.name = "newname"
first_obj.phonenr = "9898876"

second_obj.image = "new image"

first_obj.save()

first_obj.create(second_obj) #<------ can i do this?

would this save the second object? is it ever possible to do this?

Upvotes: 0

Views: 49

Answers (1)

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53981

I think you are getting confused, try this:

class ImageModel(models.Model):
    image = models.ImageField()

class MyFirstModel(models.Model):
    name = ...
    image = models.ForeignKey(Image)


> image_model_instance = ImageModel()
> image_model_instance.save()
> first_model_instance = MyFirstModel(name="foo")
> first_model_instance.image = image_model_instance
> first_model_instance.save()

There is a create() function, but it is used for creating and saving new instances of a model:

first_model_instance = MyFirstModel.objects.create(Name="foo")

so the same as:

first_model_instance = MyFirstModel()
first_model_instance.save()

Upvotes: 1

Related Questions