Thursday 15 September 2011

python - how to add a instance of a foreign key to an instance of a model that it points to -


I have a DJ ango app in which a user can schedule the command line 'jobs' to run on the remote system. Defining a job and a foreign key relationship is my main business. Class job (model. Model): name = models.CharField (max_length = 250, null = False, blank = False) user = model.carfield (max_length = 30, null = False, Blank = False) command = model.Carfild (max_length = 1000, empty = false, empty = false) when ORNON = model.DATIMEFOLD ('run date', empty = false, empty = false) output = model.carfield (max_link = 100000, Blank = True, Blank = True) Class Host (Model. Model): Job = Model. FirstGroup (Job) Name = Model.Carfild (Max_Lang = 100, Blank = False, Blank = False] Model = Boolean Field (Default = False)

Next I have a code Block in which some postal data prompts you to add some jobs to the job (host hosts represent the host name that the user wants to execute the specified command in the job charge, which is linked to the host instance). POST: If "host" is in p: # host is added to the input name attribute in the template, which is host hostname.append (for a host). Request. [P]) selected = getCheckedJobs () # A list of jobs to add this host

Now when my names are assembled and jobs add them, let me know That i do not know how to take a job example and add a host example that is associated with it Can anyone tell me how to go about this? Thanks in advance for any help.

Are you sure you have the right relationship structure? With the foreign key on the host, each job can have many hosts, but each host can have only one job. Your comment says "A list of jobs to connect this host" - it is not possible with this relationship.

If this is correct - you need to get your host example, then save your job attribute to the example of the job and host examples. Something like this:

  # assume is the select_job task example: host = Host.objects.get (name = hostname) Excluding ObjectDoesNotExist: # Do something to host an unmatched hostname here Pass host.job = selected_job host.save ()    

No comments:

Post a Comment