0

I'm learning some django basics and I have question about models.

class Album(models.Model):
    name = models.CharField(max_length=30)
    datecreated = models.DateTimeField(auto_now_add=True, blank=True)
    email = models.EmailField()

class Photo(models.Model):
    album = models.Foreignkey(Album, on_delete=models.CASCADE) 
    pic_url = models.TextField(default="http://some.url/pic.png")
    date = models.DateTimeField(auto_now_add=True, blank=True)

My goal: If you are creating new Album in admin panel it should have 30 photos by default.

Rarblack
  • 4,559
  • 4
  • 22
  • 33
  • 3
    I'm not sure what your question is here. Yes, you can have 1000 photos. Note, you don't seem to have any relationship between photo and album - presumably you want a ForeignKey there. – Daniel Roseman Nov 29 '18 at 09:19
  • Maybe, at first, read the documentation https://docs.djangoproject.com/en/2.1/howto/initial-data/ Also read https://code.djangoproject.com/wiki/Fixtures – Ihor Voronin Nov 29 '18 at 09:24
  • Possible duplicate of [Django insert default data after migrations](https://stackoverflow.com/questions/39739439/django-insert-default-data-after-migrations) – Ihor Voronin Nov 29 '18 at 09:25
  • Does that mean 30 photos max per album limit or 30 default photos (obtained from who knows where) auto inserted? Or 30 "boxes" for "drag-drop" photos on album page even if album is empty? – Ivan Starostin Nov 29 '18 at 11:44
  • @IvanStarostin Actually i want to know if Is there any way to make a function (creating 30 photos) which is gonna be called when creating album from admin panel? – unknownkoreancoder Nov 29 '18 at 12:25
  • So what is your question: how to catch a moment when Album is created or how to create an instance of a model? ps this is still looking like another XY problem and if this is for "prod" then I'd suggest to rethink the concept. – Ivan Starostin Nov 29 '18 at 12:29
  • @IvanStarostin I would like to create 30x instance of model PHOTO for every new instance of ALBUM. I'm new to coding and I'm trying my best to learn, this is just a simple project – unknownkoreancoder Nov 29 '18 at 12:34
  • Did you succeed with creating an Album? – Ivan Starostin Nov 29 '18 at 12:35
  • @IvanStarostin Yes, I can even create it from admin panel. – unknownkoreancoder Nov 29 '18 at 12:38
  • So still zero code lines written, right? how to create objects: https://docs.djangoproject.com/en/2.1/ref/models/instances/#creating-objects how to react on some object creation (and do something else): https://docs.djangoproject.com/en/2.1/ref/signals/#post-save post_save example: https://stackoverflow.com/questions/13014411/django-post-save-signal-implementation – Ivan Starostin Nov 29 '18 at 12:39
  • @IvanStarostin wow signals are nice, i didn't find them in any tutorial i've read so far. Thanks a milion! – unknownkoreancoder Nov 29 '18 at 12:53

0 Answers0