Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
[HELP] Left Join between multiple databases

I want to do a left join with another db, What would be the django equivalent of this query ?

USE PARTNER_DB;
SELECT CLIENT_ID, PARTNER.FIRST_NAME, PARTNER.LAST_NAME,
RPM, DRAWDOWN_PROTECTION, CONCAT(EMPLOYEE_DB.EMPLOYEE.FIRST_NAME, " ", EMPLOYEE_DB.EMPLOYEE.LAST_NAME),
EMPLOYEE_DB.EMPLOYEE.MAIL
FROM PARTNER
LEFT JOIN EMPLOYEE_DB.EMPLOYEE
ON RM_ID = EMPLOYEE_DB.EMPLOYEE.EMP_ID
WHERE CLIENT_ID = 'X1234'


I am using multiple databases in my project and both of these databases are not managed by django.

I tried to dig into django documentation but was unable to find a way to achieve this, instead i bumped into this
[https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#no-cross-database-relations][1].

I am sure that there will be a hack / workaround this. Looking forward to your solutions.



**These are two different db's not managed by django.**

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'PARTNER_DB',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
},
'employeedb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'EMPLOYEE_DB',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}

**My db models are**

class Partner(models.Model):
client_id = models.CharField(db_column='CLIENT_ID', primary_key=True, max_length=25)
first_name = models.CharField(db_column='FIRST_NAME', max_length=50)
last_name = models.CharField(db_column='LAST_NAME', max_length=50, blank=True, null=True)
mobile = models.CharField(db_column='MOBILE', max_length=10, blank=True, null=True)
active_from = models.DateField(db_column='ACTIVE_FROM')
rm_id = models.CharField(db_column='RM_ID', max_length=10, blank=True, null=True)
preferred_name = models.CharField(db_column='PREFERRED_NAME', max_length=50, blank=True, null=True)
rpm = models.IntegerField(db_column='RPM', blank=True, null=True)
drawdown_protection = models.IntegerField(db_column='DRAWDOWN_PROTECTION', blank=True, null=True)
depository_id = models.CharField(db_column='DEPOSITORY_ID', max_length=45, blank=True, null=True)
dob = models.DateField(db_column='DOB', blank=True, null=True)
account_type = models.CharField(db_column='ACCOUNT_TYPE', max_length=25, blank=True, null=True)
account_status = models.CharField(db_column='ACCOUNT_STATUS', max_length=25, blank=True, null=True)
first_holder_pan_number = models.CharField(db_column='FIRST_HOLDER_PAN_NUMBER', max_length=45, blank=True, null=True)
correspondence_address = models.CharField(db_column='CORRESPONDENCE_ADDRESS', max_length=1000, blank=True, null=True)
permanent_address = models.CharField(db_column='PERMANENT_ADDRESS', max_length=1000, blank=True, null=True)
nominee_name = models.CharField(db_column='NOMINEE_NAME', max_length=100, blank=True, null=True)
second_holder_name = models.CharField(db_column='SECOND_HOLDER_NAME', max_length=100, blank=True, null=True)
second_pan_number = models.CharField(db_column='SECOND_PAN_NUMBER', max_length=45, blank=True, null=True)
mail_id = models.CharField(db_column='MAIL_ID', max_length=200, blank=True, null=True)
bank_name = models.CharField(db_column='BANK_NAME', max_length=1000, blank=True, null=True)
bank_account_number = models.CharField(db_column='BANK_ACCOUNT_NUMBER', max_length=50, blank=True, null=True)
bank_micr_code = models.CharField(db_column='BANK_MICR_CODE', max_length=50, blank=True, null=True)
bank_ifsc_code = models.CharField(db_column='BANK_IFSC_CODE', max_length=50, blank=True, null=True)

class Meta:
managed = False
db_table = 'PARTNER'

class Employee
(models.Model):
emp_id = models.IntegerField(db_column='EMP_ID', primary_key=True)
first_name = models.CharField(db_column='FIRST_NAME', max_length=100)
last_name = models.CharField(db_column='LAST_NAME', max_length=100)
mail = models.CharField(db_column='MAIL', max_length=100)

class Meta:
managed = False
db_table = 'EMPLOYEE'


[1]: https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#no-cross-database-relations


/r/django
https://redd.it/6r2kwt
[AF] POST request on one of multiple defined endpoints returns 404

EDIT: The error ended up being with NGINX, all I needed to do was change the ownership of the /var/lib/nginx directory from the nginx user to the user running the Flask application. Doh

Hi, I was having the darndest time debugging this and couldn't find anything similar online so hopefully you guys can help me get to the bottom of this

I have two endpoints, a login and a post submitter. Both are GET and POST request endpoints, both are defined, both work locally. Deployed, however, the login endpoint works fine but POST requests on the post submitter endpoint always return a 404. I don't believe it's an issue with NGINX or gunicorn as the logs show nothing out of the ordinary and the login endpoint works flawlessly. I've spent almost two days debugging this and I just can't for the life of me figure it out. Relevant code snippets are posted below.

@app.route('/newpost', methods=['GET', 'POST'])
@login_required
def new_post():
form = BlogForm()
#No code in this conditional is ever actually executed, 404's first
if form.validate_on_submit():
#Omitted code for getting form data and creating form object
try:
#Omitted code for database actions
return redirect(url_for('index', _external=True))
except IntegrityError:
flash("Error inserting into database")
return('', 203)
else:
#Omitted some code for template rendering
return render_template('new_post.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
userAuth = current_user.is_authenticated
if form.validate_on_submit():
flash("Login attempt logged")
user = User.query.filter_by(username=form.userID.data).first()
if user:
checkPass = form.password.data.encode('utf-8')

if bcrypt.checkpw(checkPass, user.password):
user.authenticated = True
login_user(user, remember=form.remember_me.data)
return redirect(url_for('index', _external=True))
else:
form.errors['password'] = 'false'
else:
form.errors['username'] = 'false'

else:
return render_template('login.html',
title='Log In',
loggedIn=userAuth,
form=form)

/r/flask
https://redd.it/7p1g11