How to describe the set field in Django - django-models

I have an article object, and like to assign it some tags (I have up to 30 predefined categories)
so I need an ordered set of attributes in ArticleModel.
I tried to create several ForeignKeys to the TagsTable, but in this case user can set same tag more than once.
Is there any standard approach to store a set field in Django ORM?

Instead of ForeignKey, use ManyToManyField in your ArticleModel.

Related

Create array field in jhipster using mongodb

I want create one field e.g URL in that field we can insert multiple value url that data is stored in array format using jhipster mongodb
Created getter setter of string[]url in dto.java
With JHipster, you define your entities as per the docs.
It's worth noting that you can't define arrays of Lists directly, you have to create a one-to-many relationship, eg: set up your JDL something like this:
entity SomeEntity {
id String
...
}
entity Url {
name String
}
relationship OneToMany {
SomeEntity{url} to Url{parentEntity}
}
It's also worth noting that JHipster does not provide a uni-direction one-to-many relationship at the minute.

Convert three modelsto one single query django query

This are my model with some of the fields:
class Advertisers(models.Model):
account_manager_id = models.ForeignKey(AccountManagers, on_delete=models.CASCADE,null=True, db_column='account_manager_id',related_name="advertisers")
class AdvertiserUsers(models.Model):
user_id = models.OneToOneField('Users', on_delete=models.CASCADE,null=True,db_column='user_id', related_name='advertiser_users')
advertiser_id = models.ForeignKey('Advertisers', on_delete=models.CASCADE,null=True,db_column='advertiser_id', related_name='advertiser_users')
class Users(models.Model):
email = models.CharField(unique=True, max_length=100)
I want Id's, user ids and email of all advertisers.
Id's of all user:-
advertiser_ids = advertisers.objects.all() # can get id from here
find user_ids of advertiser_ids:
user_ids = AdvertiserUsers.objects.filter(advertiser_id__in=advertiser_ids) # can get user_id from here
find id and email using this query:
user_ids = Users.objects.filter(id__in=user_ids) # can get email from here
How to make it shorter like directly querying from Advertisers i will be able to get Users models email.
Thankyou in advance
You can filter with:
Users.objects.filter(advertiser_users__advertiser_id__isnull=False).distinct()
The .distinct() [Django-doc] will prevent returning the same Users multiple times.
You can annotate the User objects with the Advertisers primary key, etc:
from django.db.models import F
Users.objects.filter(advertiser_users__advertiser_id__isnull=False).annotate(
account_manager_id=F('advertiser_users__advertiser_id__account_manager_id'),
advertiser_id=F('advertiser_users__advertiser_id')
)
The Users objects that arise from this have a .email attribute (and the other attributes that belong to a Users object), together with a .account_manager_id and an .advertiser_id. That being said, this is probably not a good idea: the way you have modeled this right now, is that a Users object can relate to multiple Advertisers objects, so it makes not much sense to add these together.
You can for each user access the related Advertisers with:
myusers = Users.objects.filter(
advertiser_users__advertiser_id__isnull=False
).prefetch_related(
'advertiser_users',
'advertiser_users__advertiser_id'
).distinct()
for user in myusers:
print(f'{user.email}')
for advuser in user.advertiser_users.all():
print(f' {advuser.advertiser_user.pk}')
Note: normally a Django model is given a singular name, so User instead of Users.
Note: Normally one does not add a suffix _id to a ForeignKey field, since Django
will automatically add a "twin" field with an _id suffix. Therefore it should
be account_manager_id, instead of account_manager.
Advertisers.objects.all().values_list('id','account_manager_id','advertiser_users__user_id',advertiser_users__user_id__email)

Django - get queryset with id's not in set of values

According to doc -
https://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut -
I can get set of objects with specified in list ids. Is there any short way to get another set of objects, with id's not in the specified list. Blog.objects.filter(pk__not_in=[1,4,7]) - did not work for me. PS: is there any annotation of possible expresissions for filtering querysets, of making own short expressions?
Use the exclude method.
Blog.objects.exclude(pk__in=[1,4,7])
At first your query is wrong. You should write your query Blog.objects.filter(pk__in=[1,4,7]). And if you want to use not then you should read here

bulk remove the required paramater fron custom option magento

i have around 10000 products in my database which all have custom options and they all are set to required.
I need to unset required.
Please suggest me how can i do this dynamically.
I don't want to do it one by one from magento admin because that will take forever
thanks
If you want to reset the required field for all custom options of all products here is a quick & dirty way of doing it.
All the custom options are stored in the table catalog_product_option.
The column name that decides if the option is required or not is is_require.
So running this query should do the trick.
UPDATE `catalog_product_option` SET `is_require` = 0 WHERE 1
Add the table prefix if you have one.
you can update those product's required parameter by using import product by csv. I think, you have imported products by csv. simply copy that SKU and custom field option into new csv file.
And import Again those products. It will update custom option required field as you want
I'm just expanding on Marius' answer. I needed to do this but only for a bunch of custom options I'd made required by accident. I figure it might help someone else who made a similar miscalculation! :-D
UPDATE catalog_product_option o
JOIN catalog_product_option_title t ON t.option_id=o.option_id
SET o.is_require = 0
WHERE t.title = 'Additional Comments'
AND o.is_require = 1
Luckily the options I imported all had the same title so I was able to filter it that way.
If you're using SKU's for your custom options, you could probably further simplify the query as it requires no joins.
UPDATE catalog_product_option o
SET o.is_require = 0
WHERE o.sku = 'SKU1234'
AND o.is_require = 1
^ not tested that but looking at the structure of the table that should work.

cakephp setting select options and values at Model

In my database model, my attribute is set as type INT.
On the front end, I want to display a select field with representative values for the respective Integer values.
eg: [1 = Home, 2 = About]
I am currently using an external plugin for the administrating content, and the select values only allows integer. So my idea is to achieve this at respective Model. Is it possible?
Genarally yes.
You should be able to attach results of Model->find('list') to select field. Of course your model should have name or title fields for description values (Home, About).
Sounds like the kind of enum representation as I always use.
Try this solution:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
I basically uses an array matching to resolve those ints into strings in a clean way - using the model. can be the whole array for select fields or just the specific string for output in the view/index.
Its also fully form and bake-template capable.
If you name the field "attribute" in your table, and name the method "attributes()" you can easily have "cake bake" to bake this via custom templates.

Resources