I am having a subtle error with a duplicate key integrity error on django with a set of foreignkey relations.
I have the following function:
def update_relationship(actor, action, status, target):
existing = Relation.objects.filter(actor=actor, target=target)
# If this relation is 'on', turn it off
if Relation.objects.filter(actor=actor, target=target, status=status):
Relation.objects.filter(actor=actor, target=target).update(status="")
# If this relationship is not on, turn it on
else:
created = True
if existing:
existing.update(status=status)
else:
Relation.objects.create(actor=actor, target=target, status=status)
As you can see, I am testing to see if the relationship exists already in the database and then updating it if it does exist and creating a new row if it does not. However, it seems under some conditions that I can't reproduce, Django is giving me a duplicate key error, even for conditions where, as far as I can tell, there is only one instance of that.
For reference, here is the the model definition:
class Relation(models.Model):
Status = Choices(('L', 'Like', 'Like'),
('D', 'Dislike', 'Dislike'),
('S', 'Save', 'Save'))
actor = models.ForeignKey('members.Member', related_name='relations')
target = models.ForeignKey('members.Member', related_name='reverse_relations')
status = models.CharField(choices=Status, max_length=10)
created = models.DateTimeField('created', auto_now_add=True)
notified = models.BooleanField(default=False)
notified_mutual = models.BooleanField(default=False)
class Meta:
unique_together = (('actor', 'target'),)
ordering = ('created',)
verbose_name = 'Relation'
verbose_name_plural = 'Relations'
First of all, right expression to check for existence is:
existing = Relation.objects.filter(actor=actor, target=target).exists()
But the djano way to write your sentences is with get_or_create method, is that method that you are looking for:
Relation.objects.get_or_create(actor=actor, target=target,
defaults={ 'status':status }
)
Or, for your case, something like:
r, _ = Relation.objects.get_or_create(actor=actor, target=target )
r.status = '' if r.status == 'on' else status
r.save()
Related
I'm having an issue where I have a MariaDB event combined with my Django Model. For context, here is my model code:
class PartGroup(models.Model):
GroupID = models.AutoField(primary_key=True, unique=True)
Label = models.CharField(max_length=40, blank=True)
SiteID = models.ForeignKey('Site', on_delete=models.CASCADE, null=True)
InspectionPeriod = models.IntegerField(blank=False, null=True)
LastInspected = models.DateField(blank=True, null=True)
InspectionDue = models.CharField(max_length=255, blank=True)
ConnectedTo = models.ManyToManyField('self', blank=True, null=True)
The fields I want to highlight here are InspectionPeriod, LastInspected, and InspectionDue. I have another model which adds Inspections related to the GroupID. This contains a date of the inspection:
class GroupInspection(models.Model):
InspectionID = models.AutoField(primary_key=True, unique=True)
GroupID = models.ForeignKey('PartGroup', on_delete=models.CASCADE, null=True, unique=True)
class GroupReport(models.Model):
ReportID = models.AutoField(primary_key=True, unique=True)
InspectionID = models.ForeignKey('GroupInspection', on_delete=models.CASCADE, null=True)
Date = models.DateField(auto_now=False, auto_now_add=False, null=True)
Comment = models.CharField(max_length=255, blank=True)
Signature = models.CharField(max_length=255, blank=True)
I have a MariaDB event to update the LastInspected field, and from there some code in my view checks that date against the inspection period to see if an inspection is due.
Here's where the problem occurs
I have to have my MariaDB Event updating every 1 second in order to make this work reasonably well. I'm not sure if that's absolutely horrendous performance wise or not, if anyone has another solution that works smoother that's welcome. But that's not the real issue. When I create an inspection, this redirects immediately to my Group template page. This takes less than a second to redirect, which leaves my users confused as they just created an inspection and it doesn't show on the redirect yet unless they immediately refresh after 1 second.
How can I get around this?
EDIT 1
I forgot to mention - the way this works at template level is that I have a page which shows a table view of all my groups and their attributes. I then have a few buttons that create an inspection depending on which button is pressed. This just redirects back to the same page which is why the one second thing is an issue as the redirect usually takes less than a second.
EDIT 2 Here's my view:
def groupList(request, site):
status = "default"
if request.method == "POST":
list = GroupInspection.objects.filter(GroupID = request.POST.get("group"))
if not list.exists():
insp = GroupInspection.create(PartGroup.objects.get(GroupID = request.POST.get("group")))
insp.save()
if 'pass' in request.POST:
groupReport = GroupReport.create(GroupInspection.objects.get(GroupID = request.POST.get("group")), date.today(), "Pass", request.user.username)
groupReport.save()
if 'fail' in request.POST:
groupReport = GroupReport.create(GroupInspection.objects.get(GroupID = request.POST.get("group")), date.today(), "Fail", request.user.username)
groupReport.save()
if 'checkSubmit' in request.POST:
groupReport = GroupReport.create(GroupInspection.objects.get(GroupID = request.POST.get("group")), date.today(), request.POST.get("comments"), request.user.username)
groupReport.save()
status = "changed"
siteselected = Site.objects.get(SiteID = site)
groupList = PartGroup.objects.filter(SiteID = siteselected)
warnings = 0
expired = 0
good = 0
for group in groupList:
if group.LastInspected == None:
group.InspectionDue = "Yes"
expired = expired + 1
else:
Deadline = group.LastInspected + timedelta(days=group.InspectionPeriod)
if datetime.now().date() > Deadline:
group.InspectionDue = "Yes"
expired = expired + 1
elif datetime.now().date() > (Deadline - timedelta(days=30)):
group.InspectionDue = "<30 Days"
warnings = warnings + 1
else:
group.InspectionDue = "No"
good = good + 1
group.save()
context = {
'status': status,
'SiteNo': siteselected.SiteID,
'SiteName':siteselected.SiteName,
'groupList': groupList,
'expired': expired,
'warnings': warnings,
'good': good,
}
template = loader.get_template('moorings/groupList.html')
return HttpResponse(template.render(context, request))
EDIT 3 Here's my SQL Event
CREATE EVENT updateLastInspected
ON SCHEDULE EVERY 1 SECOND
DO
UPDATE proj_partgroup g INNER JOIN ( SELECT i.GroupID_id, max(r.Date) Date FROM proj_groupinspection i INNER JOIN proj_groupreport r ON r.InspectionID_id = i.InspectionID GROUP BY i.GroupID_id ) t ON g.GroupID = t.GroupID_id SET g.LastInspected = t.Date;
You can replace your SQL Event with Django signal that runs after any GroupReport update, and in case it was new report created - updates corresponding PartGroup last updated date.
proj/signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.apps import apps
GroupReport = apps.get_model("proj", "GroupReport")
PartGroup = apps.get_model("proj", "PartGroup")
#receiver(post_save, sender=GroupReport)
def update_partgroup_lastinspection(sender, instance, created, **kwargs):
if created:
# cause FKs can be null - may need to add
# null checks or try / except
pg = instance.InspectionID.GroupID
if instance.Date > pg.LastInspected:
pg.LastInspected = instance.Date
pg.save(update_fields=['Date'])
# another option to perform same update
PartGroup.objects.filter(
LastInspected__lt=instance.Date,
group_inspection__group_report=instance
).update(
LastInspected=instance.Date
)
proj/apps.py
...
class ProjConfig(AppConfig):
name = "proj"
...
def ready(self):
import proj.signals
You can skip checking that value is greater and just update right away.
Or add logic in model's save() method instead (which generally is more preferred than signals).
Previous answer - is not the case here, groupList contains changes to instances.
You are returning groupList which contains old results.
The reason for this is that while being iterated over (for group in groupList) QuerySets are evaluated with the result being cached in this QuerySet instance and when later it is used in context - this evaluated result is passed, although instances were updated in the database. QuerySets need to be run and be evaluated again to fetch fresh results.
One simple solution to reuse the QuerySet and evaluate it again - is append .all() to previous QuerySet - this will make a copy of provided QuerySet and since it is new one - it is not yet evaluated.
context = {
...
'groupList': groupList.all(),
...
}
I must be doing something very wrong or this error doesn't make any sense to me. I have an object Location:
class Location(models.Model):
class Meta:
db_table = 'location'
location_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
state = models.CharField(max_length=200)
country = models.CharField(max_length=200)
apt_number = models.CharField(max_length=200, null=True, blank=True)
street_address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
zip_code = models.IntegerField()
created = DateTimeField(auto_now_add=True)
here this is my code for inserting a new or updating an existing location record:
class LocationList(generics.ListCreateAPIView):
queryset = Location.objects.all()
serializer_class = LocationSerializer
def post(self, request, *args, **kwargs):
location_dict = request.data
if 'location_id' not in location_dict:
okStatus = status.HTTP_201_CREATED
else:
okStatus = status.HTTP_200_OK
location = Location(**location_dict)
location.save()
return Response(LocationSerializer(location).data, status=okStatus)
Inserts work fine, but everytime an update happens, I get the error "Column 'created' cannot be null". My online research seems to point me to the fact that this was a bug which has been long fixed. I expect the update to pass since the 'created' field was set to auto_now_add, which means Django should set that field once upon insert and leave it on any subsequent update. I do not know why Django is trying to set that column to null or any other value on update, because I expect Django to not update the column at all. I am using MySQL as database.
I think that your problem is in the " created column " try this steps :-
1) add null=True inside the created column
2) run :$ python"select version" manage.py makemigrations "appName or keep it empty"
3) run :$ python"V" manage.py sqlmegrate "aapName" " file version" >
check the file version in the app directory type it like this "0001"
4) run :$ python"V" manage.py migrate "appName"
after these steps ur db should be updated, last step is to remove (null=True from created column ) and start project.
So i have been researching about how to update the nested serializer with onetoonefield. However it has not been able to solve my problem. As i am still new to django rest framework, i am still inexperience about what is the problem as i never done an API before.
models.py
class Membership(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
membership = models.BooleanField(default=False)
serializers.py
class MembershipSerializer(serializers.ModelSerializer):
class Meta:
model = Membership
fields = ('membership',)
class UserSerializer(serializers.ModelSerializer):
membership = MembershipSerializer(many=False)
class Meta:
model = User
fields = ('id', 'username', 'email', 'password', 'first_name', 'last_name', 'is_staff', 'membership',)
read_only_fields = ('id',)
def create(self, validated_data):
membership_data = validated_data.pop('membership')
user = User.objects.create(**validated_data)
Membership.objects.create(user=user, **membership_data)
return user
def update(self, instance, validated_data):
instance.username = validated_data.get('username', instance.username)
instance.email = validated_data.get('email', instance.email)
instance.password = validated_data.get('password', instance.password)
instance.first_name = validated_data.get('first_name', instance.first_name)
instance.last_name = validated_data.get('last_name', instance.last_name)
instance.is_staff = validated_data.get('is_staff', instance.is_staff)
instance.save()
membership_data = validated_data.get('membership')
membership_id = membership_data.get('id', None)
if membership_id:
membership_item = Membership.objects.get(id=membership_id, membership=instance)
membership_item.membership = membership_data.get('membership', membership_item.name)
membership_item.user = membership_data.get('user', membership_item.user)
membership_item.save()
return instance
views.py
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
permission_classes = [IsAuthenticated]
def get_permissions(self):
# allow non-authenticated user to create
return (AllowAny() if self.request.method == 'POST'
else permissions.IsStaffOrTargetUser()),
screenshot of api
https://i.imgur.com/dDqthRu.png
As you can see above, my membership is null, i have no idea why so i tested with is_staff to check and it is using false like a normal Booleanfield. This has make me wonder what is wrong with my models for the membership boolean field.
Main problem
As i am using a boolean field, i was trying to get the user membership to be updated. So i try to use PUT method and the result is nothing has change after i check the membership box and click on PUT.
And if i just want to update the username, i have to check on the membership box else it will give me this:
https://i.imgur.com/KpzHIsE.png
I have been checking online for several solution and none of them has work for me with the update method. I am also puzzle by the null value in the api for the booleanfield membership.
I am trying to get SQLAlchemy to let my database's foreign keys "on delete cascade" do the cleanup on the association table between two objects. I have setup the cascade and passive_delete options on the relationship as seems appropriate from the docs. However, when a related object is loaded into the collection of a primary object and the primary object is deleted from the session, then SQLAlchemy issues a delete statement on the secondary table for the record relating the primary and secondary objects.
For example:
import logging
import sqlalchemy as sa
import sqlalchemy.ext.declarative as sadec
import sqlalchemy.orm as saorm
engine = sa.create_engine('sqlite:///')
engine.execute('PRAGMA foreign_keys=ON')
logging.basicConfig()
_logger = logging.getLogger('sqlalchemy.engine')
meta = sa.MetaData(bind=engine)
Base = sadec.declarative_base(metadata=meta)
sess = saorm.sessionmaker(bind=engine)
session = sess()
blog_tags_table = sa.Table(
'blog_tag_map',
meta,
sa.Column('blog_id', sa.Integer, sa.ForeignKey('blogs.id', ondelete='cascade')),
sa.Column('tag_id', sa.Integer, sa.ForeignKey('tags.id', ondelete='cascade')),
sa.UniqueConstraint('blog_id', 'tag_id', name='uc_blog_tag_map')
)
class Blog(Base):
__tablename__ = 'blogs'
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.String, nullable=False)
tags = saorm.relationship('Tag', secondary=blog_tags_table, passive_deletes=True,
cascade='save-update, merge, refresh-expire, expunge')
class Tag(Base):
__tablename__ = 'tags'
id = sa.Column(sa.Integer, primary_key=True)
label = sa.Column(sa.String, nullable=False)
meta.create_all(bind=engine)
blog = Blog(title='foo')
blog.tags.append(Tag(label='bar'))
session.add(blog)
session.commit()
# sanity check
assert session.query(Blog.id).count() == 1
assert session.query(Tag.id).count() == 1
assert session.query(blog_tags_table).count() == 1
_logger.setLevel(logging.INFO)
session.commit()
# make sure the tag is loaded into the collection
assert blog.tags[0]
session.delete(blog)
session.commit()
_logger.setLevel(logging.WARNING)
# confirm check
assert session.query(Blog.id).count() == 0
assert session.query(Tag.id).count() == 1
assert session.query(blog_tags_table).count() == 0
The above code will produce DELETE statements as follows:
DELETE FROM blog_tag_map WHERE
blog_tag_map.blog_id = ? AND blog_tag_map.tag_id = ?
DELETE FROM blogs WHERE blogs.id = ?
Is there a way to setup the relationship so that no DELETE statement for blog_tag_map is issued? I've also tried setting passive_deletes='all' with the same results.
Here, the “related object” is not being deleted. That would be “tags”. The blog_tags_table is not an object, it is a many-to-many table. Right now the passive_deletes='all' option is not supported for many-to-many, that is, a relationship that includes "secondary". This would be an acceptable feature add but would require development and testing efforts.
Applying viewonly=True to the relationship() would prevent any changes from affecting the many-to-many table. If the blog_tags_table is otherwise special, then you'd want to use the association object pattern to have finer grained control.
I defined some WTForms forms in an application that uses SQLALchemy to manage database operations.
For example, a form for managing Categories:
class CategoryForm(Form):
name = TextField(u'name', [validators.Required()])
And here's the corresponding SQLAlchemy model:
class Category(Base):
__tablename__= 'category'
id = Column(Integer, primary_key=True)
name = Column(Unicode(255))
def __repr__(self):
return '<Category %i>'% self.id
def __unicode__(self):
return self.name
I would like to add a unique constraint on the form validation (not on the model itself).
Reading the WTForms documentation, I found a way to do it with a simple class:
class Unique(object):
""" validator that checks field uniqueness """
def __init__(self, model, field, message=None):
self.model = model
self.field = field
if not message:
message = u'this element already exists'
self.message = message
def __call__(self, form, field):
check = self.model.query.filter(self.field == field.data).first()
if check:
raise ValidationError(self.message)
Now I can add that validator to the CategoryForm like this:
name = TextField(u'name', [validators.Required(), Unique(Category, Category.name)])
This check works great when the user tries to add a category that already exists \o/
BUT it won't work when the user tries to update an existing category (without changing the name attribute).
When you want to update an existing category : you'll instantiate the form with the category attribute to edit:
def category_update(category_id):
""" update the given category """
category = Category.query.get(category_id)
form = CategoryForm(request.form, category)
The main problem is I don't know how to access the existing category object in the validator which would let me exclude the edited object from the query.
Is there a way to do it? Thanks.
In the validation phase, you will have access to all the fields. So the trick here is to pass in the primary key into your edit form, e.g.
class CategoryEditForm(CategoryForm):
id = IntegerField(widget=HiddenInput())
Then, in the Unique validator, change the if-condition to:
check = self.model.query.filter(self.field == field.data).first()
if 'id' in form:
id = form.id.data
else:
id = None
if check and (id is None or id != check.id):
Although this is not a direct answer I am adding it because this question is flirting with being an XY Problem. WTForms primary job is to validate that the content of a form submission. While a decent case could be made that verifying that a field's uniqueness could be considered the responsibility of the form validator, a better case could be made that this is the responsibility of the storage engine.
In cases where I have be presented with this problem I have treated uniqueness as an optimistic case, allowed it to pass form submission and fail on a database constraint. I then catch the failure and add the error to the form.
The advantages are several. First it greatly simplifies your WTForms code because you do not have to write complex validation schemes. Secondly, it could improve your application's performance. This is because you do not have to dispatch a SELECT before you attempt to INSERT effectively doubling your database traffic.
The unique validator needs to use the new and the old data to compare first before checking if the data is unique.
class Unique(object):
...
def __call__(self, form, field):
if field.object_data == field.data:
return
check = DBSession.query(model).filter(field == data).first()
if check:
raise ValidationError(self.message)
Additionally, you may want to squash nulls too. Depending on if your truly unique or unique but allow nulls.
I use WTForms 1.0.5 and SQLAlchemy 0.9.1.
Declaration
from wtforms.validators import ValidationError
class Unique(object):
def __init__(self, model=None, pk="id", get_session=None, message=None,ignoreif=None):
self.pk = pk
self.model = model
self.message = message
self.get_session = get_session
self.ignoreif = ignoreif
if not self.ignoreif:
self.ignoreif = lambda field: not field.data
#property
def query(self):
self._check_for_session(self.model)
if self.get_session:
return self.get_session().query(self.model)
elif hasattr(self.model, 'query'):
return getattr(self.model, 'query')
else:
raise Exception(
'Validator requires either get_session or Flask-SQLAlchemy'
' styled query parameter'
)
def _check_for_session(self, model):
if not hasattr(model, 'query') and not self.get_session:
raise Exception('Could not obtain SQLAlchemy session.')
def __call__(self, form, field):
if self.ignoreif(field):
return True
query = self.query
query = query.filter(getattr(self.model,field.id)== form[field.id].data)
if form[self.pk].data:
query = query.filter(getattr(self.model,self.pk)!=form[self.pk].data)
obj = query.first()
if obj:
if self.message is None:
self.message = field.gettext(u'Already exists.')
raise ValidationError(self.message)
To use it
class ProductForm(Form):
id = HiddenField()
code = TextField("Code",validators=[DataRequired()],render_kw={"required": "required"})
name = TextField("Name",validators=[DataRequired()],render_kw={"required": "required"})
barcode = TextField("Barcode",
validators=[Unique(model= Product, get_session=lambda : db)],
render_kw={})
Looks like what you are looking for can easily be achieved with ModelForm which is built to handle forms that are strongly coupled with models (the category model in your case).
To use it:
...
from wtforms_components import Unique
from wtforms_alchemy import ModelForm
class CategoryForm(ModelForm):
name = TextField(u'name', [validators.Required(), Unique(Category, Category.name)])
It will verify unique values while considering the current value in the model. You can use the original Unique validator with it.
This worked for me, simple and easy:
Make sure that every time when a new row created in DB it must have unique name in colomn_name_in_db otherwise it will not work.
class SomeForm(FlaskForm):
id = IntegerField(widget=HiddenInput())
fieldname = StringField('Field name', validators=[DataRequired()])
...
def validate_fieldname(self, fieldname):
names_in_db = dict(Model.query.with_entities(Model.id,
Model.colomn_name_in_db).filter_by(some_filtes_if_needed).all())
if fieldname.data in names_in_db.values() and names_in_db[int(self.id)] != fieldname.data:
raise ValidationError('Name must be unique')