ImageField not getting deleted when save button is pressed : Django-admin - django-admin

i have the following files
models.py
Class Trip(models.Model)
featured = models.BooleanField(default=False, help_text='Tags this trip as a featured trip.')
top_ten_trip = models.BooleanField(default=False)
header_image = models.ImageField(help_text='This is the image shown when viewing the details for this trip.', upload_to='images/', blank=True, null=True)
map_image = models.ImageField(help_text='The map image for this trip.', upload_to='images/', blank=True, null=True)
.....
and so on
admin.py
class TripAdmin(admin.ModelAdmin):
list_display = ('name', 'code', 'category', 'supplier', 'active', 'featured', 'top_ten_trip',)
search_fields = ('name', 'code',)
list_filter = ('category', 'featured', 'top_ten_trip',)
filter_horizontal = ('countries', 'related_trips',)
The field appear with browse button in admin something like this
Header image: Currently: images/comfort_japan.jpg
Change: Delete this file
Map image: Currently: images/map_japan_.jpg
Change: Delete this file
Now the Problem :-
When i click on Delete this imagefile( images/comfort_japan.jpg ) it get removed for that instant but does not deleted when i press save button on admin.
I want when i click on delete this file and press save it should get deleted.
What mistake i am doing or what am i missing ?
Thanks in advance.

This was intentionally changed in Django 1.3, see
Django Ticket #15224.
You need to implement file deletion yourself. Check related posts:
How do I get Django Admin to delete files when I remove an object from the database/model?
Django admin: Add a "remove file" field for Image- or FileFields

Hi All I solved my problem by using snippet 1633
http://djangosnippets.org/snippets/1633/ .It works pretty fine.

Related

How do I make dropdown roles in pycord?

As many may know, discord.py has stopped being maintained. As a result of this, I switched over to Pycord.
My question is:
How do I make the dropdown menu do roles?
I looked at the example on the Github Example file for making dropdown menus in Pycord, and I attempted to make a dropdown menu (successfully). However, this only sends a message when an option is selected from the menu. I want to use this menu for adding and removing roles. How would I go about doing that?
class Select(discord.ui.Select):
def __init__(self):
options = [
discord.SelectOption(label="role name", description="Add the Role_Name role"), #These are the labels
discord.SelectOption(label="role name 2", description="Add role name role."),#^
]
super().__init__(placeholder="Waiting for selection...", max_values=1, min_values=1, options=options)#what will be written waiting for a choise
async def callback(self, interaction: discord.Interaction):
if self.values[0] == 'role name': #'role name'must be the same as the label u want the action to happen. Ex: if label name == hi, well uchange role name to hi here
role = interaction.guild.get_role(ROLE_ID_HERE)#add ur role ID here
await interaction.user.add_roles(role)#Basically if the interaction happen, itll add the role role^ to the user
elif self.values[0] == 'role name 2':
rolee = interaction.guild.get_role(ROLE_ID_HERE)#same as up
await interaction.user.add_roles(rolee)
class SelectView(discord.ui.View):
def __init__(self, *, timeout):
super().__init__(timeout=timeout)
self.add_item(Select())
#client.command()
async def droproles(ctx):
select = SelectView(timeout=None)
await ctx.send("Choose ur role here", view=select)
This is the code. If there is an error tell me, i didnt test it, but it should work. If u dont understand something, tell me. U need to understand to be able to re do it sometime

How to change a modx revolution users class_key when newly created VIA the manager?

I'm trying to change a new user's class_key value when they are created VIA the manager.
In a plugin that fires on the onUserSave event:
<?php
$uid = $_POST['id'];
$sql = "update modx_users set `class_key` = 'extUser' where id = $uid;";
$modx->log(modX::LOG_LEVEL_ERROR, 'query = ' . $sql);
$query = $modx->query($sql);
return;
Which works when you EDIT an EXISTING user, but does not work if you try to CREATE a NEW user.
Any thoughts on how to do this?
None of the system events look like they fire when a new user is created.
Looking at the user/create processor, you would need to listen for OnUserFormSave which is fired after saving the new user.
I haven't tested this, but in your plugin you have access to $modx->event. Log the output of this for the OnUserFormSave event, hopefully it should include a 'data' property containing the new user object. The object should contain the new user id.
Let me know how that goes!
Update
I've tested this out. As expected, you have access to the new user id, user object (and much more) in the $modx->event object:
if ($modx->event->name == 'OnUserFormSave') {
// id of user you've just created
$newUserId = $modx->event->name->params->id;
// full modUser object is available here
$newUserObj = $modx->event->name->params->user;
}
In plugin with OnUserSave event:
$scriptProperties['user']->set('class_key','extUser');
That's it nothing more.
You cannot call
$scriptProperties['user']->save();
in OnUserSave as other examples elsewhere have shown.

two same attributes in POST

I need to upload file with Yii. In view I have row:
<?php echo CHtml::activefileField($qualificationModel, 'picture'); ?>, where
$qualificationModel = new SkillsMapping;
Part of controller:
$skillsModel = SkillsMapping::model();
$skillsModel->attributes=$_POST['SkillsMapping'];
$skillsModel->picture=CUploadedFile::getInstance($skillsModel,'picture');
echo var_dump($_FILES);
if($skillsModel->validate())
{
if($skillsModel->save())
{
$skillsModel->picture->saveAs('images/qual-pics');
$this->redirect(array('view','id'=>$model->user_id));
}
}
When i chose photo and clicked button i have a error Picture cannot be blank.
After checking POST request i founded strange thing - request have two attributes - SkillsMapping[picture]= and SkillsMapping[picture]=Lighthouse.jpg. If commented echo in top of this post, both attributes disappear. How to remove empty SkillsMapping[picture] and why it's going? Thanks.
You do not need to remove the empty hidden field because it's there to help you, not hurt you (Yii itself automatically puts it there; see the relevant part of the source).
The hidden field is there so that $_POST is populated with an empty value when no file has been selected to upload. If a file has been selected the file input control will provide its own POST value that overwrites the empty "guard value".

How to Save Multipart Form in Grails; Text and File Upload

I'm trying to submit a form entry with an uploaded file, but I can't seem to get the controller to save the data properly.
Essentially, I want to post a caption and the uploaded source in one form.
An example of my domain class:
class Image {
String caption
Date dateCreated
Date lastUpated
String source
}
I don't how to store the source file and save the entry.
Here is what I've done so far:
def upload () {
def f = request.getFile('source')
f.transferTo(new File("/path/to/file.tmp"))
return
}
def save () {
upload()
def img = new Image(params)
img.save(flush: true)
...runtime exception...
}
File creation works, but obviously the details on saving the Image entry is incorrect.
Consider this question answered. The problem was not Grails, but the tiny detail of me failing to install the plug-in necessary to insert data into the database.
See this source code Grails file upload example or this presentation Uploading files with Grails.
Hope this helps

SPListItem.Update and SPFile.Update fail

We are running one MOSS application as below:-
1. user use InfoPath request form to trigger workflow attached to SharePoint document library
2. all sub-sequent tasks are performed by users by InfoPath task form within SharePoint site through "Workflow Task" list (open, checkout, approve or reject) till the task completed.
3. all request form submitted could be viewed as xml file in the document library through "Explorer View"
My quesiton is why I cannot update the item located in the document library, basically open the SPListItem:-
SPSite thisSite = new SPSite("http://server")
{
SPWeb thisWeb = thisSite.OpenWeb("/web")
{
thisSite.AllowUnsafeUpdates = true;
thisWeb.AllowUnsafeUpdates = true;
SPDocumentLibrary library = (SPDocumentLibrary)thisWeb.Lists["DocLib"];
foreach (SPListItem item in library.Items)
{
SPFile file = item.File;
I tried almost all ways I could find:-
item["Customer Name"] = "123456";
item.Update();
// or item.SystemUpdate();
file.Item["Customer Name"] = "123456";
file.Update();
After each update, the value resumed when reopen or visit through the same code lines!
but when I view item properties, the "Last modified" is changed
Last modified at 6/8/2010 12:27 AM by Administrator
This do drive me cray, any help, please.
Thanks & best regards,
Leon
When you publish your form to a list, it asks you to choose the fields to be shown on that list. As you choose the item you want to update, click "Modify". Check the checkbox
at the bottom saying "Allow user to edit data in this field by using a datasheet or property page". This works for me.

Resources