Copy ManyToMany Value - python-3.x

I am currently trying to copy a many-to-many-field from one model to another but running into a bit of trouble. I have been able to create the model fine with a many-to-many-field with a ModelMultipleChoiceField, and the model saves the way I want it to. When I try to copy it to another model, I don't get an error, but nothing happens. Here is the code that I have tried:
author = Author.objects.create(author=self.author)
author = Author.objects.all()[0]
book = entry.approvers.all()
author.pk = None
author.readers.add(*self.readers.all())
I researched this all am and see that M2M models can be tricky. I've tried several variations of the code above and nothing works. I worked my way through several must be an instance errors this am, and that's how I discovered the author = Author.objects.all()[0] command. I no longer get an error but my many-to-many values are not being copied over to another model either. Thanks for any help.
I found this reference on SO and it seems to be what would help me but I tried it and it doesn't work. I am using generic class based views which is probably what is causing me a bit of additional grief.
Class Based Views (CBV), CreateView and request.user with a many-to-many relation
From the example, I used this template for my code but to no avail.
I get needs to have a value for field "id" before this many-to-many relationship can be used.
I just found this reference, Django. Create object ManyToManyField error
It works, and I can then use the following command to post one of the M2M values, but how can I do this without hard coding the pk?
book.reader.add('1')
The above works, but when I try something like
book.reader.add(*self.readers.all())
I've researched this further and this...
author = Author.objects.create(author=self.author)
Plus this alone works...
book.reader.add('1')
Just need to figure out how to replace '1' with a variable name.
After more investigation this may be related to the following issue?
Django: IntegrityError during Many To Many add()
It would seem maybe there is some kind of bug with the bulk_create? The individual add with a hardcoded number works just fine. When I try with the (*self.readers.all()) I don't receive an error message but the manytomany reference is not copied over to the over table. I've seen several articles that say that the commands I'm using work just fine, but perhaps it's for something other than PostgreSQL.
Thanks for any thoughts.
As a newbie, I was doing this work in SAVE. Once I moved it to post_save, the following format worked as expected...
#receiver(post_save, sender='Test.Book')
def post_save(sender,instance, **kwargs):
author = book.objects.create(subject=instance.book)
author.approvers.add(*instance.approvers.all())

As a newbie, I was doing this work by overriding SAVE in my model instead of doing this work in a post_save signal. Changing my approach fixed my issue. The code associated with my correct approach is..
#receiver(post_save,sender='Test.Book')
def post_save(sender,instance,**kwargs):
author = book.objects.create(subject=instance.book)
author.approvers.add(*instance.approvers.all())

Related

Weights&Biases Sweep - Why might runs be overwriting each other?

I am new to ML and W&B, and I am trying to use W&B to do a hyperparameter sweep. I created a few sweeps and when I run them I get a bunch of new runs in my project (as I would expect):
Image: New runs being created
However, all of the new runs say "no metrics logged yet" (Image) and are instead all of their metrics are going into one run (the one with the green dot in the photo above). This makes it not useable, of course, since all the metrics and images and graph data for many different runs are all being crammed into one run.
Is there anyone that has some experience in W&B? I feel like this is an issue that should be relatively straightforward to solve - like something in the W&B config that I need to change.
Any help would be appreciated. I didn't give too many details because I am hoping this is relatively straightforward, but if there are any specific questions I'd be happy to provide more info. The basics:
Using Google Colab for training
Project is a PyTorch-YOLOv3 object detection model that is based on this: https://github.com/ultralytics/yolov3
Thanks! 😊
Update: I think I figured it out.
I was using the train.py code from the repository I linked in the question, and part of that code specifies the id of the run (used for resuming).
I removed the part where it specifies the ID, and it is now working :)
Old code:
wandb_run = wandb.init(config=opt, resume="allow",
project='YOLOv3' if opt.project == 'runs/train' else Path(opt.project).stem,
name=save_dir.stem,
id=ckpt.get('wandb_id') if 'ckpt' in locals() else None)
New code:
wandb_run = wandb.init(config=opt, resume="allow",
project='YOLOv3' if opt.project == 'runs/train' else Path(opt.project).stem,
name=save_dir.stem)

Unable to create or update an Asset\Image when saving a Pimcore object

My goal is to create / update objects by using a csv file. csv file is fine and allready heavy used.
I wrote a complete script for that, most setSomething() works very well with text, structured Datas etc.. but i'm facing a trouble with the Images. I read the doc twice...
https://pimcore.com/docs/4.6.x/Development_Documentation/Objects/Object_Classes/Data_Types/Image_Types.html
$image = Asset\Image::getByPath("/examples/example1.jpg");
$object->setImage($image);
$object->save();
and that exactly what's i did to create / update $article
$marque = Pimcore\Model\Asset\Image::getByPath("/Catalog/Marques/".$image);
$article->setEarg($marque);
$article->save();
please note that :
$image exists,
$marque is found, and is an instance of Asset\Image
setEarg($marque) is properly defined to set an Image data field
in UI all works fine
cache cleared
Is it possible i misunderstood something ?
I also searched in passed Issues, no luck.
https://github.com/pimcore/pimcore/issues
Maybe someone did face the same problem ? Anc could a hint on how to solve that ?
Regards,
I'm confused...this code is working as it should.
It seems to a cache problem, even if i cleared it. In the UI image did appear after some minutes / hour. Now it appear immedialtly. This question can be closed, but this push me to another one... how did this cache / deffer problem happened. If i find something i will update here.

Import Failure - Role With Id Does Not Exist

I am getting an import error in a specific environment with a managed CRM 2011 solution. The solution has been imported before into many other environments, but the one in particular where it is failing is throwing the following error:
Dependency Calculation
role With Id = 9e2d2d9b-645f-409f-b31d-3a9c39fcc340 Does Not Exist
I am a bit confused about this. I searched within the solution XML and was not able to find any reference to this particular GUID of 9e2d2d9b-645f-409f-b31d-3a9c39fcc340. I cannot really find it in SQL either, just wandering through the different tables, but perhaps I do not know exactly where to look there.
I have tried importing the solution multiple times. As a desperation effort, I tried renaming all of the security roles in the destination environment prior to importing, but this did not help.
Where is this reference to a security role actually stored? Is this something that is supposed to be within my solution--which my existing CRM deployment is expecting me to import?
How do I fix the problem so that I am able to import this solution?
This is the code we used to fix the issue. We had to run two different scripts. Script A we had to run a total of four times. Run it once, attempt the import, and then consult the log to find the role that is causing the problem--if you receive another error for another role.
To run script A, you must use a valid RoleTemplateId from your database. We just picked a random one. It should not matter precisely which one you use, because you will erase that data element with script B.
After all of the roles are fixed, we got a different error (complaining something about the RoleTemplateId was already related to a role), and had to run script B. That removes the RoleTemplateId from multiple different roles and sets it to NULL.
Script A:
insert into RoleBaseIds(RoleId)
values ('WXYZ74FA-7EA3-452B-ACDD-A491E6821234')
insert into RoleBase(RoleId
,RoleTemplateId
,OrganizationId
,Name
,BusinessUnitId
,CreatedOn
,ModifiedOn
,CreatedBy
)
values ('WXYZ74FA-7EA3-452B-ACDD-A491E6821234'
,'ABCD89FF-7C35-4D69-9900-999C3F605678'
,(select organizationid from Organization)
,'ROLE IMPORT FIX'
,(select BusinessUnitID from BusinessUnit where ParentBusinessUnitId is null)
,GETDATE()
,GETDATE()
,null
)
Script B:
update RoleBase
set RoleTemplateId = NULL
where RoleTemplateID='ABCD89FF-7C35-4D69-9900-999C3F605678'
Perfect solution, worked for me! My only comment would be the error in Script B: it shouldn't clear the template IDs of all roles for the given template, only the template ID of the newly created "fix" role, as follows:
update RoleBase
set RoleTemplateId = NULL
where RoleID='WXYZ74FA-7EA3-452B-ACDD-A491E6821234'
I would've gladly put this in a comment to the answer, but not enough rep as of now.

How do you delete or rename content parts in Orchard?

I gave a content part a wrong name and I want to rename it. I tried renaming the classes and all calls of it in the solution to no avail. I even tried deleting entries from the ShellFeatures table and the table generated by Migrations itself, but that just made things worse. Now the whole module's features aren't being recognized. Anyone tried this before?
I know this is an old question but I just had to tackle this and this is the first result I landed on from Google.
You can delete the ContentPart and add it again using migrations for example:
public int UpdateFrom3() {
// remove the AccreditationsPart part cleanly
ContentDefinitionManager.DeletePartDefinition(typeof (AccreditationsPart).Name);
enter code here
// re-add the AccreditationsPart again
ContentDefinitionManager.AlterPartDefinition(typeof (AccreditationsPart).Name,
cfg = > cfg
.Attachable()
.WithDescription("Adds a row of configurable accreditations to the content type"));
return 4;
}
You would need to customise the update numbers to fit your current migrations.cs and also change the AlterPartDefinition to match whatever you want it to be.
WARNING: This is only intended for fixing problems that you catch straight away. When you delete the part in the first section of the migration you will lose any associated data. If you already have the code running in production then you will have to use a different approach.

A2K Error 3011 on TransferSpreadsheet method

I'm jut trying to import a spreadsheet into a table in Access 2000.
The spreadsheet is called cc-ledgcodebalances.xls with no field names.
My code is
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "tmptmp", VFileName, 0
This seemed to work once but since then I now get this;
The Microsoft Jet database engine could not find the object
''cc-ledgcodebalances$''. Make sure the object exists and that you
spell its name and the path name correctly. 3011
Google searches aren't really turning up much that is useful. I'm trying to
import the whole thing and I don't think this is to do with named ranges.
Bit stumped by this, any help?
I answered this elsewhere that you asked it:
The error you're getting often occurs when the saved import spec is out of synch with either the source data or the target table. Try starting the import with the wizard, then load the saved import spec and it will likely throw an error or reveal what's no longer accurate.
I just had a similar problem under 2007 using direct range references instead of named ranges. It seems that dollar signs in the range reference can cause this error. Using Replace() on "$" and replacing with "" solved this issue in my case.

Resources