I'm trying to update one field of IgThread EmbeddedDocument, and error occurs:
'IgThread' object has no attribute 'save'
I've tried some weird solutions, non of them work.
There is bug in current code
Mutation:
def mutate(self, _, **kwargs):
ig_pk = kwargs['ig_pk']
thread_input = kwargs['ig_thread']
lead_ = Lead.objects(ig__pk=ig_pk).first()
for thread in lead_.messages.ig:
Lead.objects(
ig__pk=ig_pk,
messages__ig__thread_id=thread_input.thread_id,
).update(
push_all__messages__ig__S__messages=new_messages,
)
thread.last_activity_at = thread_input.last_activity_at
thread.save()
Parent:
class Lead(Document):
id = fields.ObjectIdField()
messages = fields.EmbeddedDocumentField(Messages)
Nested EmbeddedDocument:
class Messages(EmbeddedDocument):
ig = fields.EmbeddedDocumentListField(IgThread)
Deeper:
class IgThread(EmbeddedDocument):
thread_id = fields.StringField()
last_activity_at = fields.StringField()
I want to update last_activity_at
Pls, help, I have to fix that bug and no one cant help mi with this in the office :<
Solved! It was easy...
def mutate(self, _, **kwargs):
ig_pk = kwargs['ig_pk']
thread_input = kwargs['ig_thread']
lead_ = Lead.objects(ig__pk=ig_pk).first()
for thread in lead_.messages.ig:
Lead.objects(
ig__pk=ig_pk,
messages__ig__thread_id=thread_input.thread_id,
).update(
push_all__messages__ig__S__messages=new_messages,
)
lead_ = Lead.objects(ig__pk=ig_pk).first() <--------
thread.last_activity_at = thread_input.last_activity_at
lead_.save() <--------
Related
I have an app contains these models
class Transaction(models.Model):
chp_reference = models.CharField(max_length=50, unique=True)
rent_effective_date = ..
income_period = ..
property_market_rent =..
number_of_family_group = ..
cruser = ..
prop_id = ..
state = ..
group =..
class FamilyGroup(models.Model):
name = models.CharField(..
transaction =models.ForeignKey(Transaction,..
...
class FamilyMember(models.Model):
transaction = models.ForeignKey(Transaction, ..
family_group = models.ForeignKey(FamilyGroup..
name = models.CharField..
date_of_birth = models.DateField..
....
Im trying to make Imports app that will accept xlsx files with some certain format.
after i imported the models from the other apps, therefore i've created a model that have a field for each field i n the above models , i removed a lot so it look readable.
im trying to make it update_or_create since i think its the best approach to do, since maybe in future maybe i want to update some fields. I have created the first update_or_create for Transaction but since family_group and family_member are childs of Transaction and Inlines i cant figure out how to apply this. the main idea is i have a transaction contains family_groups and family_members inside it .
class Batch(models.Model):
batch = models.CharField(max_length=50)
transaction_chp_reference = models.CharField(unique=True)
transaction_rent_effective_date = models.DateField(..
transaction_property_market_rent = models.DecimalField(..
transaction_number_of_family_group = models.PositiveSmallIntegerField(..
family_group_name = models.CharField(..
family_group_family_type = models.CharField(..
family_group_alloc_id = models.PositiveIntegerField(..
family_group_last_rent = models.DecimalField(..
family_member_name = models.CharField(..
family_member_contact_id = models.PositiveIntegerField(..
family_member_surname = models.CharField(..
family_member_partnered = models.BooleanField(..
def __str__(self):
return str(self.batch)
def save(self, *args, **kwargs):
self.message = ''
if self.transaction_chp_reference:
trans, t = Transaction.objects.update_or_create(
# filter on the unique value of `chp_reference`
chp_reference=self.transaction_chp_reference,
# update these fields, or create a new object with these values
defaults={
'income_period':self.transaction_income_period,
'property_market_rent':self.transaction_property_market_rent,
'number_of_family_group':self.transaction_number_of_family_group,
'rent_effective_date':self.transaction_rent_effective_date,
'cruser':self.transaction_cruser,
'prop_id':self.transaction_prop_id,
'state':self.transaction_state,
}
)
self.message += 'Transaction "' + str(trans.chp_reference) + '" Created\n'
obj, mt = MaintenanceType.objects.update_or_create(
name=self.family_group_maintenance_type,
)
obj, ft = FamilySituation.objects.update_or_create(
name= self.family_group_family_type,
)
obj, fg = FamilyGroup.objects.update_or_create(
transaction=t,
name=self.family_group_name,
defaults={
'alloc_id':self.family_group_alloc_id,
'any_income_support_payment':self.family_group_any_income_support_payment,
'cra_amount':self.family_group_cra_amount,
'cra_eligibilty':self.family_group_cra_eligibilty,
'family_type':ft,
'ftb_a':self.family_group_ftb_a,
'ftb_b':self.family_group_ftb_b,
'last_rent':self.family_group_last_rent,
'maintenance_amount':self.family_group_maintenance_amount,
'maintenance_type':mt,
'name':self.family_group_name,
'number_of_additional_children':self.family_group_number_of_additional_children,
}
)
self.message += 'Family Group "' + str(obj.name) + '" Created\n'
now im getting an error when try to import xlsx file:
Cannot assign "False": "FamilyGroup.transaction" must be a "Transaction" instance.
Traceback:
Traceback (most recent call last):
File "E:\15-12\venv\lib\site-packages\django\db\models\query.py", line 575, in update_or_create
obj = self.select_for_update().get(**kwargs)
File "E:\15-12\venv\lib\site-packages\django\db\models\query.py", line 417, in get
self.model._meta.object_name
calculator.models.FamilyGroup.DoesNotExist: FamilyGroup matching query does not exist.
UPDATE
I have replaced the save() method with this code.
#receiver(post_save, sender=Batch)
def post_save_tranaction(sender, instance, created, **kwargs):
message = ''
if created:
Transaction.objects.update_or_create(
chp_reference=instance.transaction_chp_reference, defaults=
{rent_effective_date':instance.rent_effective_date,... , ... })
## now since FamilyGroup is a child (Foreignkey) to Transaction
## im not sure how to make it instance of Transaction
## FamilyMember is also a child of FamilyGroup and Transaction - same issue
## i tried this --->
transactions = []
transaction = Transaction.objects.all()
for i in transaction:
transactions.append(i.pk)
FamilyGroup.objects.update_or_create(name=instance.family_group_name,
transaction__in=transactions
)
I am relatively new to Django, but not to python, My model is trying to use a class (defined in a separate file) in which data is coming from a REST API, the retrieved data is in a nested dictionary. The code will run fine in python, but when I try it in Django (makemigrations), I get an error:
File "c:\blah-blah\Clone_PR.py", line 20, in GetFoundOnSelectItems
values = self._issueEdit["fields"]["customfield_13940"]["allowedValues"]
TypeError: 'NoneType' object is not subscriptable
I tried using type hints, but that does not work either.
models.py
from dal import autocomplete
from django.db import models
from django.contrib import messages
from .Login import jlogin
from .Jira_Constants import ProductionServer, TestServer, StageServer
from .Clone_PR import Issue
jira = None
issue = Issue()
class ClonePrLogin(models.Model):
username = models.CharField(max_length=30)
password = models.CharField(max_length=30)
#classmethod
def LoginToJira(cls):
global jira
jira = jlogin(ProductionServer, cls.username, cls.password)
class PrEntry(models.Model):
prToClone = models.CharField(max_length=20)
#classmethod
def GetIssueAndMeta(cls):
global issue
issue.initialize(jira, cls.prToClone)
class ClonePr(models.Model):
issueKey = issue.issueKey
issue.GetFoundOnSelectItems()
foundOnList = issue.foundOnSelectItems
foundOn = autocomplete.Select2ListChoiceField(choice_list=foundOnList)
Clone_PR.py
from typing import List, Dict
class Issue():
def __init__(self):
self.jiraInst = None
self.issueKey = ''
self._issue = None
self._issueEdit = None
# self._issueEdit = Dict[str, Dict[str, Dict[str, List[Dict[str, str]]]]]
self.foundOnSelectItems = []
def initialize(self, jira, prKey):
self.jiraInst = jira
self.issueKey = prKey
self._issue = jira.issue(prKey)
self._issueEdit = jira.editmeta(prKey)
def GetFoundOnSelectItems(self):
values = self._issueEdit["fields"]["customfield_13940"]["allowedValues"]
items = [x["value"] for x in values]
self.foundOnSelectItems = items
In Django, running makemigrations will load all the modules. You said you're familiar with Python so you should know that the declarations inside the class:
class ClonePr(models.Model):
issueKey = issue.issueKey
issue.GetFoundOnSelectItems()
foundOnList = issue.foundOnSelectItems
foundOn = autocomplete.Select2ListChoiceField(choice_list=foundOnList)
will run when the modules load. You're calling issue.GetFoundOnSelectItems() at that time, which in turn calls values = self._issueEdit["fields"]["customfield_13940"]["allowedValues"], except that self._issueEdit = None upon the initiation of instance Issue above with this line: issue = Issue().
I highly recommend you spend some time to become more familiar with how Django starts up an app. The module-level and nested model declarations here are both antipatterns and may cause data issues in the future.
So basicaly I have this class:
class Grupa(db.Model):
__tablename__ = 'grupa'
id_grupy = db.Column(db.BigInteger, db.Sequence('grupa_seq'), primary_key=True)
nr_semestru = db.Column(db.SmallInteger)
rodzaj = db.Column(db.String(5))
nr_grupy = db.Column(db.SmallInteger)
dzien_tyg = db.Column(db.SmallInteger)
godz_rozp = db.Column(db.SmallInteger)
ilosc_godz = db.Column(db.SmallInteger)
czestatliwosc = db.Column(db.SmallInteger)
id_prowadzacego = db.Column(db.ForeignKey('prowadzacy.id_prowadzacego'))
id_przedmiotu = db.Column(db.ForeignKey('przedmiot.id_przedmiotu'))
id_sali = db.Column(db.ForeignKey('sala.id_sali'))
id_specjalnosci = db.Column(db.ForeignKey('specjalnosc.id_specjalnosci'))
id_studia = db.Column(db.ForeignKey('studia.id_studia'))
oferty = db.relationship('Oferta', backref='grupa')
def __repr__(self):
return '<Grupa(grupa={self.id_grupy!r})>'.format(self=self)
Now using flask I would like to create an object of it and add it to the database. I found the next example of:
from yourapp import User
me = User('admin', 'admin#example.com')
db.session.add(me)
db.session.commit()
The problem I have is
here
To be hones I was expecting giving params like "id_grupy" and so on and I have this. What is this classname and bases here?
P.S. That was really helpfull but now I got diffrent problem.
I go in for over my dataframe to extract it row by row and then add it to my database and there is that weird error
for row in df.iterrows():
dzien, godz, ilosc, tyg, id_naucz, id_sala, id_prz, rodz, grupa, id_st, sem, id_spec = row
As You see im trying to put this values from each row and error i get is :
dzien, godz, ilosc, tyg, id_naucz, id_sala, id_prz, rodz, grupa, id_st, sem, id_spec = row
ValueError: not enough values to unpack (expected 12, got 2)
Which is weird I just gave 12 values for him, why he got only 2 of them?
What you did is nice, but you need to add a __init__ function to your Grupa class in order to give attributes you want to your object :
def __init__(self, id_grupy, nr_semestru, rodzaj, ...):
self.id_grupy = id_grupy
self.nr_semetru = nr_semetru
self.rodzaj = rodzaj
...
(see an example here)
See ya !
I am using haystack and elasticsearch. I am building indexes in the following manner-->
class BookIndex(indexes.SearchIndex,indexes.Indexable):
text= indexes.CharField(document=True,use_template=True)
content_auto = indexes.EdgeNgramField(model_attr='title',boost=1.5)
isbn_13 = indexes.CharField(model_attr='isbn_13')
category = indexes.CharField()
sub_category = indexes.CharField()
def prepare_sellers(self, Book):
return [seller.name for seller in Book.sellers.all()]
def prepare_category(self, Book):
return [Book.category.name]
def prepare_sub_category(self, Book):
return [Book.sub_category.name]
And I have included the following in the settings file :-
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
But when I am going to add data to my database by doing -> http://dpaste.com/01739SW , haystack index updating is failing and I am getting the following error-->http://dpaste.com/2YGXZ8J
Can someone please help me out in fixing the issue. Thank you.
I'm having trouble getting a model to save (or be put()) correctly. The interesting part is that a model doing a very similar save before it works. Below are the relevant parts of the code. At the two logging points the first correctly returns the email of the user. However, the second one results in the error AttributeError: 'NoneType' object has no attribute 'c_user'. Obviously the setting and un-setting of the variables in this is not the correct way to do things, I've just added these to hunt down the problem to discover that the model isn't being saved. Any suggestions? Thank you much!
class Source(db.Model):
current_user = db.UserProperty()
class SourceMember(db.Model):
c_user = db.UserProperty()
x_position = db.IntegerProperty()
y_position = db.IntegerProperty()
...
user = users.get_current_user()
if user:
source_key = self.request.get('g')
if not source_key:
source_key = user.user_id()
source = Source(key_name = source_key,
current_user = user)
source.put()
else:
source = Source.get_by_key_name(source_key)
source = None
source = Source.get_by_key_name(source_key)
logging.warning(source.current_user)
if source:
sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
if not sourceMember:
sourceMember = SourceMember(parent = source.key(),
key_name = user.user_id() + source_key,
c_user = user,
x_position = None,
y_position = None)
sourceMember.put()
sourceMember = None
sourceMember = SourceMember.get_by_key_name(user.user_id() + source_key)
logging.warning(sourceMember.c_user)
When you create the SourceMember you're giving it a parent, but then when you get it, the parent is missing. Source doesn't have a parent, so getting it just from its id works.