On Mon, Apr 6, 2015 at 5:32 PM Tiglath Suriol <tiglathsur...@gmail.com> wrote:
> > On Monday, April 6, 2015 at 3:02:30 PM UTC-4, Flavia Missi wrote: >> >> Hello Tiglath, >> >> On Monday, April 6, 2015 at 1:06:43 PM UTC-3, Tiglath Suriol wrote: >>> >>> >>> Environments have servers; servers have applications; applications can >>> be on various servers, and applications have files. >>> >>> - servers must be unique within environments, >>> - applications must be unique on each server. >>> - files must be unique within an applications. >>> >>> Will this accomplish it? >>> >> >> You're almost there, you just have one relationship wrong: (I'll also add >> some refactoring suggestions for your attribute names) >> >> >>> >>> >>> class Environment(models.Model): >>> >>> ''' Environment can be "Production", "QA" or "CT" (customer >>> test).''' >>> >>> env_name = models.CharField('Environment Name', max_length=20) >>> >> *env_name* could be only *name* for simplicity, in this case the class >> Environment (or the environment object) works as a context, >> so when you're accessing the *name* attribute it'll be cleaner, e.g: >> >> environ.name # better than environ.env_name >> >> >>> >>> class Server(models.Model): >>> >>> environment = models.ForeignKey(Environment) >>> >>> server_name = models.CharField('Server Name', max_length=20, >>> primary_key=True) >>> >> >> same here, use *name *instead. >> >> >>> >>> >>> class Application(models.Model): >>> >>> servers = models.ManyToManyField(Server) >>> >>> app_name = models.CharField('Application Name', max_length=50, >>> primary_key=True) >>> >> >> same here, use *name *instead. >> >> >>> >>> >>> class File(models.Model): >>> >>> applications = models.ManyToManyField(Application) >>> >> >> If you need unique files for application, you should be using a >> ForeignKey field instead of a ManyToMany. >> >> >>> file_name = models.CharField('File Name', max_length=100, >>> primary_key=True) >>> >> >> same here, use *name *instead. >> >> >>> >>> >>> Thanks >>> >>> >>> >>> >>> >>> >>> >> >> As far as I see everything else looks fine. >> >> Hope it helps! >> > > > Thank you kindly for your reply. > > Actually I have "name" but added the prefixes to the post for clarity. > OH, nevermind then x) > I need files to be unique within an application, but a file with name say, > "config" could exist in different applications. > > Am I right to think that if I use ForeignKey in the File class, as you > suggest, it will let me just have one file named 'config' overall, but if I > use ManyToMany with the File name as primary key, there will be only one > 'config' per Application, but I can have many files named 'config,' say, > one per app? > You're right and wrong at the same time: by making a field unique you're preventing two records being created with the same value on the unique field (*name* in your case). It doesn't depend on other relationships the [File] object might have. What you might wanna use in this case is unique_together <https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together>, using the id and the name as inputs. Regards, -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADQG5okFysrEVV6xmU00fO%3DTZ5XjZpwx_YbjkjZC5TyHwEt4Ag%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.